Description

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.

Discuss

先求出该矩阵的skyline,这样可以得到每个节点的skyline最小值,遍历矩阵的每一个元素,与最小值的差值就是增加建筑物高度的总和。

Code

class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) { return 0; }
int m = grid.length;
int n = grid[0].length;
int[] col = new int[m];
int[] row = new int[n];
for (int i = 0; i < m; i++) {
int max = Integer.MIN_VALUE;
for (int j = 0; j < m; j++) {
if (grid[i][j] > max) { max = grid[i][j]; }
}
col[i] = max;
}
for (int i = 0; i < n; i++) {
int max = Integer.MIN_VALUE;
for (int j = 0; j < m; j++) {
if (grid[j][i] > max) { max = grid[j][i]; }
}
row[i] = max;
}
int sum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int tmp = Math.min(col[i], row[j]);
if (grid[i][j] <= tmp) { sum += tmp - grid[i][j]; }
}
}
return sum;
}
}

【Leetcode】807. Max Increase to Keep City Skyline的更多相关文章

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

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

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

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

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

  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

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

  7. 【LeetCode】485. Max Consecutive Ones 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  8. 【LeetCode】1004. Max Consecutive Ones III 解题报告(C++)

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

  9. 【LeetCode】149. Max Points on a Line 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...

随机推荐

  1. JavaScript专题

    1. 在ajax的任何回调方法中,比如success回调,使用return,将会无效 2. //todo

  2. 笔记本win8系统共享wifi上网方法

    华硕笔记本电脑,安装了win8系统,使用wifi上网,由于连接无线路由的机器太多,超过路由连接数上限,因此转为使用笔记本共享wifi方式给手机上网. 最终上网方式为: 笔记本网卡接入无线路由器上网,笔 ...

  3. Ubuntu 配置java环境变量

    1.使用如下命令,打开/etc/profile: $sudo vi /etc/profile 2.进入编辑模式,在末尾添加: #developer enviroment, add by myself ...

  4. luogu4566 [Vani有约会]雨天的尾巴

    题目 线段树合并的板子题目了,写一写对线段树合并的理解 首先线段树合并就是把一大堆权值线段树合并起来的算法 尽管复杂度看起来并不是非常科学,但是确是非常优秀的\(O(nlogn)\) 主要的写法两种 ...

  5. cblas_sgemm cblas.h

    BLAS(Basic Linear Algebra Subprograms)库,是用Fortran语言实现的向量和矩阵运算库,是许多数值计算软件库的核心, 但也有一些其它的包装, 如cblas是C语言 ...

  6. 【luogu P1801 黑匣子_NOI导刊2010提高(06)】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1801 替罪羊树吼啊! #include <cstdio> #include <cstrin ...

  7. 【luogu P2024 食物链】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2024 摘吊打集训队的九日dalao一句话 关于带有多个相对集合的全集,我们可以多开几倍的空间.每一倍的元素表 ...

  8. C# Pascal 命名规则

    在以前版本的Visual Studio中,微软曾建议使用匈牙利命名法来写代码,并鼓励开发这位写出统一格式的代码而使用相同的法则.在最近发布的.NET和它的编程语言中,微软更换了他的这一法则.如果你用过 ...

  9. [Windows]ping itsafe&环境变量

    (1)when you ping a computer from itsafe,the ping command should return the local IP address. (2)wind ...

  10. rabbitmq消息中间件读后感

    1:RabbitMQ是一个开源的消息代理和队列服务器,可以通过基本协议在完全不同的应用之间共享数据,使用Erlang语言开发的,是基于AMQP(高级消息队列协议)协议,Erlang主要用于交换机的开发 ...