【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 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 < grid.length = grid[0].length <= 50.
- All heights grid[i][j] are in the range [0, 100].
- 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.
Discuss
先求出该矩阵的skyline,这样可以得到每个节点的skyline最小值,遍历矩阵的每一个元素,与最小值的差值就是增加建筑物高度的总和。
Code
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) { return 0; }
int m = grid.length;
int n = grid[0].length;
int[] col = new int[m];
int[] row = new int[n];
for (int i = 0; i < m; i++) {
int max = Integer.MIN_VALUE;
for (int j = 0; j < m; j++) {
if (grid[i][j] > max) { max = grid[i][j]; }
}
col[i] = max;
}
for (int i = 0; i < n; i++) {
int max = Integer.MIN_VALUE;
for (int j = 0; j < m; j++) {
if (grid[j][i] > max) { max = grid[j][i]; }
}
row[i] = max;
}
int sum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int tmp = Math.min(col[i], row[j]);
if (grid[i][j] <= tmp) { sum += tmp - grid[i][j]; }
}
}
return sum;
}
}
【Leetcode】807. Max Increase to Keep City Skyline的更多相关文章
- 【LeetCode】807. Max Increase to Keep City Skyline 解题报告(Python &C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- Leetcode 807. Max Increase to Keep City Skyline
class Solution(object): def maxIncreaseKeepingSkyline(self, grid): """ :type grid: Li ...
- 【LeetCode】485. Max Consecutive Ones 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】1004. Max Consecutive Ones III 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法/双指针 日期 题目地址:https://le ...
- 【LeetCode】149. Max Points on a Line 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...
随机推荐
- 启动eclipse出现“Error opening registry key 'software\Javasoft\Java Runtime Environment'”
启动eclipse出现“Error opening registry key 'software\Javasoft\Java Runtime Environment'”,“java was start ...
- 【java开发系列】—— JDOM创建、修改、删除、读取XML文件
有很多中操作XML文件的方法,这里介绍一下JDOM的使用方法和技巧. JDOM下载地址 创建XML文档 XML文件是一种典型的树形文件,每个文档元素都是一个document元素的子节点.而每个子元素都 ...
- Spark天堂之门解密
本课主题 什么是 Spark 的天堂之门 Spark 天堂之门到底在那里 Spark 天堂之门源码鉴赏 引言 Spark 天堂之门就是SparkContext,这篇文章会从 SparkContext ...
- 长大DeepMind第一次团队作业
1.队名 长大DeepMind 2.队员风采 学号 姓名 擅长的技术 编程的兴趣点 希望承担的角色 一句话宣言 B20150304508 晏司举 JAVA,ssm框架,MySQL数据库 JAVA后台服 ...
- LeetCodeOJ刷题之12【Integer to Roman】
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 给于用户Agent权限设置
问题:有一个用户需要有create\alter\drop job人权限.默认是只有sysadmin成员才有这个权限.肯定不能将用户放到这个组 答案:所有JOB都属于msdb库中读取和写入信息.所以,肯 ...
- react中性能优化的点
react提升代码性能的点 1.绑定如果改变作用域点话放在constructor里面做,这样可以保证整个程序的作用域绑定操作只会执行一次,而且避免子组件的无谓渲染. 2.内置的setState是个异步 ...
- Oracle文本导入器
将文本格式化为Tab分隔的数据行,可以用Excel搞定 如果新增列需要从前面的列获取数据,比如用身份证号计算年龄 需要保证新增列在导入的文件中实际存在列,否则无法使用相对位置和绝对位置进行取值 相对位 ...
- Html5简单描述(优点与缺点)
什么是HTML5 HTML5指的是包括HTML.CSS和JavaScript在内的一套技术组合.它希望能够减少网页浏览器对于需要插件的丰富性网络应用服务(Plug-in-Based Rich Inte ...
- P2852 [USACO06DEC]牛奶模式Milk Patterns
link 这是一道后缀匹配的模板题 我们只需要将height算出来 然后二分一下答案就可以了 #include<cstdio> #include<algorithm> #inc ...