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. mysql sleep 死锁例子

    表结构 CREATE TABLE `orders` ( `order_id` int(11) NOT NULL, `order_addr` varchar(255) DEFAULT NULL ) EN ...

  2. scroll js 原生

    1.当前位置滚动: document.documentElement.scrollTop 当前位置: 有可能是0 window.scrollTo(,document.documentElement.s ...

  3. python学习-数据类型

    计算机处理的数据不单纯的指数字,计算机可以处理数字.文本.音频.视频等等各种数据,下面描述的是Python中可以直接使用和处理的基本数据类型.     整数 Python可以处理任意大小的整数,跟ja ...

  4. 转载 如何使用批处理 动态改变path实现改变JDK版本

    http://www.cnblogs.com/xdp-gacl/p/5209386.html 1 @echo off 2 3 rem --- Base Config 配置JDK的安装目录 --- 4 ...

  5. CentOS7连接无线网络

    背景  CentOS7.6最小化安装,没有网线,幸好有无线网卡.下面我们直接进入主题.  附:安装教程 主题--连接无线网络 最小化安装后,裸机没有ifconfig,没有iw,最可怕的是没有网线 但我 ...

  6. java-web服务器-tomcat

    HTTP服务器: 一.定义: 1)是一个安装在服务端的[资源文件调度软件] 二.作用: 1)自动解析接收的[请求协议包]内容 2)自动在服务端计算机上定位[被访问的文件] 3)自动创建[Http响应协 ...

  7. 使用curl出现,curl: /usr/local/lib/libssl.so.1.1: version `OPENSSL_1_1_1' not found (required by /usr/lib/x86_64-linux-gnu/libcurl.so.4)

    主要原因是curl找不到openssl的路径,所以只要将openssl的路径添加到相应的变量中就可以了. 参考连接https://blog.csdn.net/RookieWutongshu/artic ...

  8. Date 对象

    JavaScript Date 对象 Date 对象 Date 对象用于处理日期与时间. 创建 Date 对象: new Date() 以下四种方法同样可以创建 Date 对象: var d = ne ...

  9. linux系统常用命令(一)

    管理 在UNIX/linux系统中,一切皆为文件:若非文件,则为进程.首先认识文件系统: linux文件系统 /var - 经常变化的(variable)文件,诸如日志或数据库等 /usr - 包含绝 ...

  10. Kettle 事务、转换内顺序、excel模版、使用踩坑

    kettle中转换和作业的执行顺序:     1.一个作业内的转换,是顺序执行的.     2.一个转换内的步骤是并行执行的.     3.作业内不支持事务,转换内支持事务. 根据业务需要,通常需要在 ...