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 ...
随机推荐
- vue函数防抖和节流
Vue函数防抖和节流https://zhuanlan.zhihu.com/p/72363385 <template> <div> <input type='text' v ...
- join 按两个文件的相同字段合并
1.命令功能 join对每一对具相同内容的输入行,合并为一行输出.默认情况是把输入的第一个字段作为连接字段,字段间用空格隔开. 2.语法格式 join option file1 file2 jo ...
- java并发学习--第一章 线程的创建
所谓的并发就是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行.所以我们看似几个线程在同时进行,其实在操作系统中 ...
- 三栏布局的三个典型方法(圣杯、双飞翼、flex)
聊聊三栏布局----左右定宽,中间自适应. 效果图: 圣杯布局 <!DOCTYPE html> <html> <head lang="en"> ...
- windows 使用 git 客户端
git客户端下载地址:https://www.git-scm.com/ tortoisegit下载地址:https://tortoisegit.org/ 双击下载的安装包,默认安装直到完成. 打开gi ...
- cenos中的软件安装
在linux中安装flash: http://jingyan.baidu.com/article/fa4125accdeeec28ad709252.html linux java环境的搭建:
- U盘安装win8(win7)+centos7双系统
centos7除了之后,就像尝鲜看看,但是发现安装之后会失去win8启动项.导致重装系统,经过反复折腾,终于搞定了,发出来共享下.默认你的 window系统已经安装好,不介绍window安装过程.本文 ...
- php array_combine()函数 语法
php array_combine()函数 语法 作用:通过合并两个数组来创建一个新数组,其中的一个数组是键名,另一个数组的值为键值.dd马达价格 语法:array_combine(keys,valu ...
- php substr_replace()函数 语法
php substr_replace()函数 语法 作用:替换字符串中某串为另一个字符串大理石平台价格 语法:substr_replace(string,replacement,start,lengt ...
- 【CF1243B1】Character Swap (Easy Version)【思维】
题意:给你两个字符串,问是否存在交换方案使得两个字符串变成一样的,方案为只交换一次且只交换s1与s2里的一个字符 题解:若一开始就相同,则存在交换方案 若一开始不同的位置为1个或大于2个,则不存在方案 ...