作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/max-increase-to-keep-city-skyline/description/

题目描述

In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts can be different for different buildings). Height 0 is considered to be a building as well.

At the end, the “skyline” when viewed from all four directions of the grid, i.e. top, bottom, left, and right, must be the same as the skyline of the original grid. A city’s skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. See the following example.

What is the maximum total sum that the height of the buildings can be increased?

Example:

Input: grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
Output: 35
Explanation:
The grid is:
[ [3, 0, 8, 4],
[2, 4, 5, 7],
[9, 2, 6, 3],
[0, 3, 1, 0] ] The skyline viewed from top or bottom is: [9, 4, 8, 7]
The skyline viewed from left or right is: [8, 7, 9, 3] The grid after increasing the height of buildings without affecting skylines is: gridNew = [ [8, 4, 8, 7],
[7, 4, 7, 7],
[9, 4, 8, 7],
[3, 3, 3, 3] ]

Notes:

  1. 1 < grid.length = grid[0].length <= 50.
  2. All heights grid[i][j] are in the range [0, 100].
  3. All buildings in grid[i][j] occupy the entire grid cell: that is, they are a 1 x 1 x grid[i][j] rectangular prism.

题目大意

这个题很符合年前北京的漏出天际线的活动啊~这个题意思是,有一个矩阵代表了现在所有房子的高度,我们想提高每个房子的高度,同时保证其在前后左右四个方向观察到的天际线的高度是不变的。问我们增加多少楼层高度的和。

解题方法

题目已经给了我们比较清楚的测试用例,通过测试用例中给的思想也能看出来,我们完全可以构造一个新的矩阵,代表着能增加高度之后的各个楼层的高度。下面讨论增加楼层高度的方式。既然我们要求每个楼层观察到的各个方向的天际线的高度是不变的,那么我们让其增加到其所在行的最高天际线和其所在列的最高天际线的最小值。比如,

题目中我们可以得出每行的天际线的高度是[8, 7, 9, 3],每列的天际线的高度是[9, 4, 8, 7]。那么,gridNew =

__|_9__4__8__7__
8 | 8, 4, 8, 7
7 | 7, 4, 7, 7
9 | 9, 4, 8, 7
3 | 3, 3, 3, 3

代码:

class Solution(object):
def maxIncreaseKeepingSkyline(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
gridNew = [[0] * len(grid[0]) for _ in range(len(grid))]
top = [max(grid[rows][cols] for rows in range(len(grid))) for cols in range(len(grid[0]))]
left = [max(grid[rows][cols] for cols in range(len(grid[0]))) for rows in range(len(grid))]
for row, row_max in enumerate(left):
for col, col_max in enumerate(top):
gridNew[row][col] = min(row_max, col_max)
return sum(gridNew[row][col] - grid[row][col] for row in range(len(left)) for col in range(len(top)))

二刷,没有创建新的数组,直接在原地进行判断。

class Solution(object):
def maxIncreaseKeepingSkyline(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
if not grid or not grid[0]: return 0
M, N = len(grid), len(grid[0])
rows, cols = [0] * M, [0] * N
for i in range(M):
rows[i] = max(grid[i][j] for j in range(N))
for j in range(N):
cols[j] = max(grid[i][j] for i in range(M))
res = 0
for i in range(M):
for j in range(N):
res += min(rows[i], cols[j]) - grid[i][j]
return res

C++版本的代码如下:

class Solution {
public:
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
int M = grid.size(), N = grid.size();
vector<int> rows(M), cols(N);
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
rows[i] = max(rows[i], grid[i][j]);
cols[j] = max(cols[j], grid[i][j]);
}
}
int res = 0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
res += min(rows[i], cols[j]) - grid[i][j];
}
}
return res;
}
};

日期

2018 年 4 月 4 日 —— 清明时节雪纷纷~~下雪了,惊不惊喜,意不意外?
2018 年 12 月 2 日 —— 又到了周日

【LeetCode】807. Max Increase to Keep City Skyline 解题报告(Python &C++)的更多相关文章

  1. Leetcode 807 Max Increase to Keep City Skyline 不变天际线

    Max Increase to Keep City Skyline In a 2 dimensional array grid, each value grid[i][j] represents th ...

  2. LeetCode #807. Max Increase to Keep City Skyline 保持城市天际线

    https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/ 执行用时 : 3 ms, 在Max Increase to Ke ...

  3. Leetcode 807. Max Increase to Keep City Skyline

    class Solution(object): def maxIncreaseKeepingSkyline(self, grid): """ :type grid: Li ...

  4. LC 807. Max Increase to Keep City Skyline

    In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...

  5. [LeetCode&Python] Problem 807. Max Increase to Keep City Skyline

    In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...

  6. 【Leetcode】807. Max Increase to Keep City Skyline

    Description In a 2 dimensional array grid, each value grid[i][j] represents the height of a building ...

  7. [LeetCode] Max Increase to Keep City Skyline 保持城市天际线的最大增高

    In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...

  8. [Swift]LeetCode807. 保持城市天际线 | Max Increase to Keep City Skyline

    In a 2 dimensional array grid, each value grid[i][j]represents the height of a building located ther ...

  9. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

随机推荐

  1. nginx二级域名指向不同文件项目配置

    需要使用泛域名解析, 并且加上空的判断,以保证没有二级域名的也可以访问 核心配置 server_name ~^(?<subdomain>.+)\.caipudq\.cn$;if ( $su ...

  2. 非线性回归支持向量机——MATLAB源码

    支持向量机和神经网络都可以用来做非线性回归拟合,但它们的原理是不相同的,支持向量机基于结构风险最小化理论,普遍认为其泛化能力要比神经网络的强.大量仿真证实,支持向量机的泛化能力强于神经网络,而且能避免 ...

  3. C语言中的重要位运算

    1. 常用的等式 :-n = ~(n-1) = ~n + 1. 2. 获取整数n的人进制形式中的最后1个,也就是只保留最后一个1,其余的全部置位0,如1000 0011 --->  0000 0 ...

  4. HDFS【hadoop3.1.3 windows开发环境搭建】

    目录 一.配置hadoop3.1.3 windows环境依赖 配置环境变量 添加到path路径 在cmd中测试 二.idea中的配置 创建工程/模块 添加pom.xml依赖 日志添加--配置log4j ...

  5. Maven 学习第一步[转载]

    转载至:http://www.cnblogs.com/haippy/archive/2012/07/04/2576453.html 什么是 Maven?(摘自百度百科) Maven是Apache的一个 ...

  6. Android EditText软键盘显示隐藏以及“监听”

    一.写此文章的起因 本人在做类似于微信.易信等这样的聊天软件时,遇到了一个问题.聊天界面最下面一般类似于如图1这样(这里只是显示了最下面部分,可以参考微信等),有输入文字的EditText和表情按钮等 ...

  7. Output of C++ Program | Set 14

    Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...

  8. Mysql不锁表备份之Xtrabackup的备份与恢复

    一.Xtrabackup介绍 MySQL冷备.热备.mysqldump都无法实现对数据库进行增量备份.如果数据量较大我们每天进行完整备份不仅耗时且影响性能.而Percona-Xtrabackup就是为 ...

  9. InnoDB的行锁模式及加锁方法

    MYSQL:InnoDB的行锁模式及加锁方法 共享锁:允许一个事务度一行,阻止其他事务获取相同数据集的排他锁. SELECT * FROM table_name WHERE ... LOCK IN S ...

  10. 使用AOP思想实现日志的添加

    //1.创建日志表syslog------->创建日志的实体类--------->在web.xml中配置监听 <listener>     <listener-class ...