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.

Runtime: 8 ms, faster than 49.01% of C++ online submissions for Max Increase to Keep City Skyline.

保存横竖最大值的最小值,减去当前值即可。

#include <algorithm>
class Solution {
public:
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
int r = grid.size();
int c = grid[].size();
vector<int> rgrid(r,);
vector<int> cgrid(c,);
for(int i=; i<r; i++) rgrid[i] = *max_element(grid[i].begin(), grid[i].end());
for(int i=; i<c; i++){
int maxval = ;
for(int j=; j<r; j++){
maxval = max(maxval, grid[j][i]);
}
cgrid[i] = maxval;
}
//for(auto x : cgrid) cout << x << endl;
//for(auto x : rgrid) cout << x << endl;
int ret = , tmp = ;
for(int i=; i<r; i++){
for(int j=; j<c; j++){
tmp = min(rgrid[i],cgrid[j]) - grid[i][j];
//cout << tmp << " " << grid[i][j] << endl;
ret += tmp > ? tmp : ;
}
}
return ret;
}
};

LC 807. Max Increase to Keep City Skyline的更多相关文章

  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&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 ...

  4. 【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 ...

  5. 【LeetCode】807. Max Increase to Keep City Skyline 解题报告(Python &C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. Leetcode 807. Max Increase to Keep City Skyline

    class Solution(object): def maxIncreaseKeepingSkyline(self, grid): """ :type grid: Li ...

  7. [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 ...

  8. [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 ...

  9. BZOJ1628: [Usaco2007 Demo]City skyline

    1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 256  Solved: 210[Su ...

随机推荐

  1. Nginx自动加载配置文件方案

    nginx自动加载配置文件方案一.nginx+consul+consul-template实现过程:consul作为服务发现软件,consul-template作为nginx配置文件的模板,consu ...

  2. 【异常】Phoenix异常:java.lang.ArithmeticException: Rounding necessary

    1 异常sql upsert into WMBIGDATA.ODS_ES_CHARGING_STATION(id,evcosType,address,serviceTel,supportOrder,o ...

  3. 【2017-04-19】C#中String.Format格式使用

    例子: int a =9; string s= a.ToString("000"); Console.Write(s); 打印出来就是009 C#格式化数值结果表 字符 说明 示例 ...

  4. 轻松阅读linux内核源码

    神器:基于linux4.4版本,含函数和功能注释树 http://www.bricktou.com/ https://lwn.net/ 从0开始写linux,这个无敌牛逼,简单阅读了Introduct ...

  5. 微信小程序开发(二)创建一个小程序页面

    为了方便讲解,我们将上篇博客创建的小程序除了project.config.json和sitemap.json两个文件保留,其他全部删除(这两个文件存的是小程序的创建信息,删掉会有报错提示). 接下来我 ...

  6. 很有用的shell脚本

    基础知识 expect基础知识 exp_continue是匹配一行后,从当前expect块第一行开始匹配 expect块的每一行匹配后,直接退出当前expect块,往下一个expect块开始匹配 ex ...

  7. 一个javascript面试题

    javascript面试题代码: <script type="text/javascript"> function fun(x,y){ console.log(&quo ...

  8. 小程序UI设计(10)-巧用模板,事半功倍

    工具中为小程序员们准备了符合微信开发规范的模板.之前帖子中介绍的规范都在模板中已经设计好了,可以直接复制粘贴使用.下图中的样式是从模板直接复制过来的.实际使用时只要更换为自己的图片和文字即可.自动生成 ...

  9. Android异常与性能优化相关面试问题-内存管理面试问题详解

    内存管理机制概述: 分配机制:操作系统会为每一个进程分配一个合理的内存大小,从而保证每一个进程能够正常的运行,不至于内存不够使用,或者某个进程占用过多的内存. 回收机制:在系统内存不足的时候,系统有一 ...

  10. spring cache会默认使用redis作为缓存吗?

    web项目中,只需要配置 redis 的IP,端口,用户名和密码就可以使用redis作为缓存了,不需要在在java 代码中配置redisConfig,redisConfig只是作为缓存配置的辅助,比如 ...