作者: 负雪明烛
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. SpringBoot整合Shiro 四:认证+授权

    搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 shiro整合Mybatis见:SpringBoot整合 ...

  2. A Child's History of England.7

    After the death of Ethelbert, Edwin, King of Northumbria [公元616年,隋朝末年], who was such a good king tha ...

  3. day14 linux三剑客之sed命令

    day14 linux三剑客之sed命令 sed命令 Sed 主要用来自动编辑一个或多个文件.简化对文件的反复操作.编写转换程序等. sed(流式编辑器) : sed主要用来修改文件. 1.sed命令 ...

  4. 【Reverse】每日必逆0x02

    BUU SimpleRev 附件 https://files.buuoj.cn/files/7458c5c0ce999ac491df13cf7a7ed9f1/SimpleRev 题解 查壳 拖入iad ...

  5. 2016广东工业大学新生杯决赛 A-pigofzhou的巧克力棒

    题目:GDUTOJ | pigofzhou的巧克力棒 (gdutcode.cn) 之前看了大佬博客的题解,一直没懂(我太菜了),后来听了朋友@77的讲解,终于懂了. 和拆分出2的n次方不一样,这是一种 ...

  6. mysql explain using index condition

    Using where:表示优化器需要通过索引回表查询数据:Using index:表示直接访问索引就足够获取到所需要的数据,不需要通过索引回表:Using index condition:在5.6版 ...

  7. 数据源(Data Source

    数据源(Data Source)顾名思义,数据的来源,是提供某种所需要数据的器件或原始媒体.在数据源中存储了所有建立数据库连接的信息.就像通过指定文件名称可以在文件系统中找到文件一样,通过提供正确的数 ...

  8. HUD总结

    HUD 指示器/HUD/遮盖/蒙板 半透明的指示器如何实现 指示器的alpha = 1.0; 指示器的背景色是半透明的 1. 创建颜色 直接创建对应的颜色 + (UIColor *)blackColo ...

  9. class.getName()和class.getSimpleName()的区别

    根据API中的定义: Class.getName():以String的形式,返回Class对象的"实体"名称: Class.getSimpleName():获取源代码中给出的&qu ...

  10. Git初始化及仓库创建和操作

    一.基本信息配置 1.全局配置用户名 git config --global user.name "YeHuan-byte" 2.全局配置邮箱 git config --globa ...