LeetCode #807. Max Increase to Keep City Skyline 保持城市天际线
https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/
执行用时 : 3 ms, 在Max Increase to Keep City Skyline的Java提交中击败了95.31% 的用户 内存消耗 : 37.7 MB, 在Max Increase to Keep City Skyline的Java提交中击败了92.13% 的用户
最直接的思路:
- 获取矩阵的每一行最大值和每一列最大值
- 根据上述最大值来“提高”建筑
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
int result = 0;
int rowSize = grid.length;
// rowMax 每行最大数值
int[] rowMax = new int[rowSize]; int colSize = grid[0].length;
// colMax 每列最大数值
int[] colMax = new int[colSize]; int i = 0;
int j = 0; for (i = 0; i < rowSize; i++) {
rowMax[i] = 0;
for (j = 0; j < colSize; j++) {
if (grid[i][j] > rowMax[i]) {
rowMax[i] = grid[i][j];
}
}
} for (j = 0; j < colSize; j++) {
colMax[j] = 0;
for (i = 0; i < rowSize; i++) {
if (grid[i][j] > colMax[j]){
colMax[j] = grid[i][j];
}
}
} for (i = 0; i < rowSize; i++) {
for (j = 0; j < colSize; j++) {
if (rowMax[i] > colMax[j]) {
result += colMax[j] - grid[i][j];
} else {
result += rowMax[i] - grid[i][j];
}
}
} return result;
}
}
LeetCode #807. Max Increase to Keep City Skyline 保持城市天际线的更多相关文章
- 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
class Solution(object): def maxIncreaseKeepingSkyline(self, grid): """ :type grid: Li ...
- [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 ...
- 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】807. Max Increase to Keep City Skyline 解题报告(Python &C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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
Description In a 2 dimensional array grid, each value grid[i][j] represents the height of a building ...
- [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 ...
- BZOJ1628: [Usaco2007 Demo]City skyline
1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 256 Solved: 210[Su ...
随机推荐
- HTML水平居中和垂直居中的实现方式
父元素是块元素,根据子元素不同分为以下几种: 1.子元素是行内元素: a.水平居中:在父元素上设置text-align:center; b.垂直居中:在行内子元素上设置行高与父元素相同line-hei ...
- wenzhang
作者:周公子链接:https://zhuanlan.zhihu.com/p/94960418来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 2006年2月9日,首都经济 ...
- qt 视频播放
可以播出视频,待完善 player = new QMediaPlayer(this); playlist = new QMediaPlaylist(); playlist->addMedia(Q ...
- Swagger详解(SpringBoot+Swagger集成)(转)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/ai_miracle/article/de ...
- php函数漏洞
1.ereg — 正则表达式匹配 此函数遇 %00 截断. <?php $a = $_GET['pwd']; var_dump(ereg ("^[0-9]+$", $a)); ...
- shelve模块 xml模块
# import shelve# f=shelve.open('db.shl')# # f['stu1']={'name':'alex1','age':28}# # f['stu2']={'name' ...
- Oracle RAC运维所遇问题记录二
oracle12c RAC源端与Dataguard目标端实时同步,因业务需求需要在源端增加PDB 1. 源端添加PDB CREATE PLUGGABLE DATABASE kdlxpdb admin ...
- HashMap的几种遍历方式(转载)
今天讲解的主要是使用多种方式来实现遍历HashMap取出Key和value,首先在java中如果想让一个集合能够用for增强来实现迭代,那么此接口或类必须实现Iterable接口,那么Iterable ...
- 在Mac电脑上使用NTFS移动硬盘遇到问题
1.sudo nano/etc/fstab 回车 输电脑密码 2.进入文本插入页面 编入: LABEL=硬盘名字 NONE ntfs rw,auto,nobrowse 3.ctrl + X 退出 选 ...
- 容易混淆的JavaScript基础知识之语法部分
type 属性: 默认的 type 就是 javascript, 所以不必显式指定 type 为 javascript javascript 不强制在每个语句结尾加 “:” , javascript ...