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 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 a1 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的更多相关文章
- 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 保持城市天际线
https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/ 执行用时 : 3 ms, 在Max Increase to Ke ...
- [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 ...
- 【LeetCode】807. Max Increase to Keep City Skyline 解题报告(Python &C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode 807. Max Increase to Keep City Skyline
class Solution(object): def maxIncreaseKeepingSkyline(self, grid): """ :type grid: Li ...
- [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 ...
- [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 ...
- BZOJ1628: [Usaco2007 Demo]City skyline
1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 256 Solved: 210[Su ...
随机推荐
- fastadmin 金额 字段类型及html验证
金额 字段类型 整数 小数 decimal 10 2 float 10 2 html验证 <div class="form-group&q ...
- Jetson TX1 安装ROS操作系统
直接按照官网上的步骤安装即可,其中会出现很多bug,主要是依赖库安装的问题,添加清华源和中科大源,(注意:中科大源会有些问题)需要apt-get update 和 apt-get upgrade更新库 ...
- 1.RPC原理学习
1.什么是RPC:远程过程调用协议 RPC(Remote Procedure Call Protocol)— 远程过程调用协议,是一种通过网络从远程计算机程序上请求服务,而不需要 了解底层网络技术的协 ...
- ble ic
ti cc25xxnordic nrf24xx nrf51xx nrf52xx Beken bk34xx
- Hive压缩和存储(十二)
压缩和存储 1. Hadoop压缩配置 1) MR支持的压缩编码 压缩格式 工具 算法 文件扩展名 是否可切分 DEFAULT 无 DEFAULT .deflate 否 Gzip gzip DEFAU ...
- zencart分类页每页显示产品数量自定义选择的方法
zencart默认分类页每页显示产品数量是固定的,如何让顾客可以选择每页显示的产品的数量呢?效果图 方式一:全部展示 方式二:下拉菜单 修改方法 1.导入sql INSERT INTO configu ...
- mysqldump恢复
mysqldump的恢复操作比较简单,因为备份的文件就是导出的SQL语句,一般只需要执行这个文件就可以了,可以通过以下的方法. 方法一 [root@zstedu andyxi3306]# mysql ...
- 【agc004d】Teleporter
题目大意 一棵树,改变一些边的父亲,使得深度不超过k. 解题思路 我一开始就想到了贪心,结果莫名其妙的把这种方法给否决了, 然后考虑优化树形dp,然后优化失败⊙﹏⊙ 贪心思路很简单,也很好感受出来,从 ...
- Python之subprocess模块的使用
1.subprocess调用系统的命令 #!/usr/bin/env python # -*- coding: utf-8 -*- import subprocess import sys compl ...
- 微信公众号开发不能使用session原因
今天做微信公众号开发整合功能的时候,使用session保存记录.用postman测试好使,但是一旦用手机就不好使.上网查了好久才明白,微信开发是不能用session的.具体原因如下:因为微信的所有请求 ...