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 a 1 x 1 x grid[i][j] rectangular prism.
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
//找到垂直每一行,与水平每一行的最高天际线
int[] vertical = new int[grid.length];
int[] horizontal = new int[grid.length];
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid.length;j++){
if(grid[i][j]>vertical[i]){
vertical[i] = grid[i][j];
}
}
}
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid.length;j++){
if(grid[j][i]>horizontal[i]){
horizontal[i] = grid[j][i];
}
}
}
int max = 0;
//遍历一遍,将每一行的最高天际线,与每一列的最高天际线进行比较,最低的值减去矩阵的值总和就是答案。
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid.length;j++){
max += horizontal[j]>vertical[i]?vertical[i]-grid[i][j]:horizontal[j]-grid[i][j];
}
}
return max;
}
}

  

Leetcode 807 Max Increase to Keep City Skyline 不变天际线的更多相关文章

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

  2. Leetcode 807. Max Increase to Keep City Skyline

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

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

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

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

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

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

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

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

  9. BZOJ1628: [Usaco2007 Demo]City skyline

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

随机推荐

  1. sqlmap Windows 安装教程

    第一步:下载 python :https://www.python.org/downloads/    (这里有python各种版本,但是一般建议安装3和2.7) sqlmap:https://git ...

  2. SDKmanager的位置

    最近学习Android Studio 因为配置的问题,需要查找SDKmanager的位置 一下是查找方法: 查找到啦~

  3. Javascript——浅谈 Event Flow

    1.Javascript Events : Event Bubbling(事件冒泡) 如果事件从最特定的元素开始,则事件流中的一个阶段称为事件冒泡(DOM中可能最深的节点)然后向上流向最不特定的节点( ...

  4. Luogu P1381油滴扩展

    传送门 数据范围给的很小啊,n >= 0 && n <= 7,所以给了DFS生存的空间. 对于每一个油滴,可以说在它下一个油滴放置之前,当前的这个油滴的半径并不确定(但是对 ...

  5. Java软件工程师面试常见问题集锦之一

    1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节.抽象 ...

  6. [Swift]LeetCode53. 最大子序和 | Maximum Subarray

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  7. [Swift]LeetCode218. 天际线问题 | The Skyline Problem

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  8. Linux 工程向 Windows 平台迁移的一些小小 tips

    Linux 工程向 Windows 平台迁移的一些小小 tips VS2013 C++11 Visual Studio 2013 没有做到对 C++11 所有的支持,其中存在的一个特性就是 In-cl ...

  9. 主机名变成bogon?连不上mysql?你需要看下这篇文章

    通过navicat for mysql操作部署在虚拟机centos里面的mysql数据库时候总是出现类似于下面的提示信息: Can't connct to MySQL server on '*.*.* ...

  10. mongo 联表查询

    查询语句 db.getCollection("A表").aggregate([ { $lookup:{ from:"B表", localField:" ...