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. 学习笔记-java 多线程

    背景说明: 多线程并发与管理,是java基础知识里的重点,本文根据<java核心技术第八版>中的多线程技术的学习,对知识点进行整理:这里只对基础知识点进行简单罗列,以达到对知识点有网状关联 ...

  2. WAKE-WIN10-SOFT-MATio

    1,matio Matio is an open-source C library for reading and writing binary MATLAB MAT files. This libr ...

  3. March 22 2017 Week 12 Wednesday

    Satisfaction doesn't come from the outside, but from the inside. 满足感并非来自外界,而是来自内心. Everything that e ...

  4. 108. Convert Sorted Array to Binary Search Tree (building tree with resursion)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  5. 西汉姆VS利物浦,铁锤『拳』出击,打回原『菱』形

    一.铁锤,还是铁桶?   铁锤帮西汉姆今晚摆出4-2-3-1对阵红军利物浦的4-3-3 ,阿勒戴斯曾在主场2-1战胜赫尔城时被球迷嘘,嫌他的足球太过丑陋『2010年的时候贝尼特斯也曾经诟病阿勒戴斯带队 ...

  6. 数字(int)转字符串和字符串转数字(int)

    室友去面试,问了一个字符串转成数字的算法题,室友没搞出来,我心想,这个不是很简单的吗?于是动手在纸上画了画代码.画完后,总感觉哪里不对,最后一个个挖掘,才发现,尼玛,这到处都是坑啊---特此记录一下中 ...

  7. 网络体系结构的概念 - 网络协议TCP - 红黑联盟

    https://i.cnblogs.com/EditPosts.aspx?opt=1 网络体系结构的概念  计算机网络就是一组通过一定形式连接起来的计算机系统,它需要四个要素的支持,即通信线路和通信设 ...

  8. 使用metasploit自带模块进行端口扫描

    搜索模块: 选择查看: 设置&扫描:

  9. 【luoguP1996】【luogu-original】约瑟夫问题

    先来看题目: P1996 约瑟夫问题 题目背景 约瑟夫是一个无聊的人!!! 题目描述 n个人(n<=100)围成一圈,从第一个人开始报数,数到m的人出列,再由下一个人重新从1开始报数,数到m的人 ...

  10. C#基础 一(方法详解)

    需要知道:类和方法的关系 方法和参数修饰符 自定义方法可以有或没有参数,也可以有或没有返回值.可以被各种关键字(static.virtual.public.new等)修饰以限制其行为. C#参数修饰符 ...