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. Delphi 通过ADO连接数据库

  2. Summer training #4

    D:找到两个数 一个是另一个的整数倍(1也算) 因为N是600000 调和级数为ln(n+1) 算一下 可以直接爆 #include <bits/stdc++.h> #include &l ...

  3. jmeter 压力测试tcp

    cmd下管理员执行 jmeter 界面 英文版 中文切换以后不能执行 最多跑905个线程 线程限制 查看结果数 界面会跑死 windows环境 注意:测试1万的tcp并发连接  大爷的 window最 ...

  4. LeetCode 01 两数之和

    链接:https://leetcode-cn.com/problems/two-sum 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们 ...

  5. u-boot-2018.09 DTS上 I2C节点的解析 (转)

    这篇理下uboot上I2C总线挂载设备的整个流程. 其他总线(如SPI等)应是类同的思路. uboot 中,以max8997挂载到s3c24xx i2c总线为例, dts里面的写法如下 aliases ...

  6. Mac常用设置备忘

    1.显示隐藏文件 1>命令行方式 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏:defaults writ ...

  7. RestTemplate响应值乱码

    @Bean public RestTemplate getRestTemplate(){ RestTemplate restTemplate = new RestTemplate(); //解决中文乱 ...

  8. 伪元素::before和::after的详细介绍

    ㈠什么是伪元素? 不同的解释: ⑴伪元素是创造关于文档语言能够指定的文档树之外的抽象.例如文档语言不能提供访问元素内容第一字或者第一行的机制.伪元素允许设计师引用它们,否则这是难以办到的.伪元素还提供 ...

  9. sql 同一行中,不同结果在不同列显示

    对不同条件查询到的结果在同一行中展示 尝试写过使用","和inner join两种方式,感觉使用","的更加直观 select table1.guid, tab ...

  10. 51 Nod 1116 K进制下的大数

    1116 K进制下的大数  基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 有一个字符串S,记录了一个大数,但不知这个大数是多少进制的,只知道这个数 ...