作者: 负雪明烛
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. EXCEL ctrl+e 百变用法不只是你用的那么简单

    Excel2013版本中,新增加了一个快捷键:Ctrl+E,可以依据字符之间的关系,实现快速填充功能.一些需要使用公式或者其他功能进行解决的问题,现在只要一个快捷键就可以实现了. 用法1:快速拆解出需 ...

  2. 从零构建Java项目(Maven+SpringBoot+Git) #02 奥斯丁项目

    前两天我说要写个项目来持续迭代,有好多小伙伴都表示支持和鼓励,项目的第一篇这不就来了么~我给项目取了个名字,英文名叫做:austin,中文名叫做:奥斯丁 名字倒没有什么特别的含义,我单纯觉得这个名字好 ...

  3. apostrophe

    apostrophe 者,', 0x27, 十进制39,ASCII里的single quote (单引号) 也.one of the 'inverted commas'. 在书写上可以表示所有格.省略 ...

  4. 30个类手写Spring核心原理之环境准备(1)

    本文节选自<Spring 5核心原理> 1 IDEA集成Lombok插件 1.1 安装插件 IntelliJ IDEA是一款非常优秀的集成开发工具,功能强大,而且插件众多.Lombok是开 ...

  5. mysql触发器实例说明

    触发器是一类特殊的事务 ,可以监视某种数据操作(insert/update/delete),并触发相关操作(insert/update/delete). 看以下事件: 完成下单与减少库存的逻辑 Ins ...

  6. 使用NSURLSessionDownloadTask实现大文件下载-监听下载进度

    - 5.1 涉及知识点(1)创建NSURLSession并设置代理,通过NSURLSessionDownloadTask并以代理的方式来完成大文件的下载 //1.创建NSURLSession,设置代理 ...

  7. 2.8 GO 参数传递

    简单将GO中参数传递分为三类 数字.字符.字符串等类型 结构体 方法 GO的方法本身就是地址的入口,打印一个方法输出的是这个方法的地址 func test_func(){ //0x488a30 fmt ...

  8. InnoDB学习(三)之BinLog

    BinLog又称为二进制日志,是MySQL服务层的数据日志,MySQL所有的存储引擎都支持BinLog.BinLog记录了MySQL中的数据更新和可能导致数据更新的事件,可以用于主从复制或数据恢复.本 ...

  9. 🔥🔥🔥Flutter 字节跳动穿山甲广告插件发布 - FlutterAds

    前言 Flutter 已成为目前最流行的跨平台框架之一,在近期的几个大版本的发布中都提到了 Flutter 版本 Google 广告插件 [google_mobile_ads] .对于"出海 ...

  10. thinkphp引入PHPExcel类---thinkPHP类库扩展-----引入没有采用命名空间的类库

    最近项目中遇到引入PHPExcel第三方类库 但是下载的phpExcel类没有命名空间,而且所有接口文件的命名都是以.php结尾  而不是tp中的.class.php 解决办法很简单:在引入没有采用命 ...