(Medium) Max Increase to Keep City SkyLine LeetCode

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.

Solution:

class Solution {
    public int maxIncreaseKeepingSkyline(int[][] grid) {

        if(grid==null|| grid.length==0 || grid[0].length==0){
            return 0;
        }

        int sum = 0;

        int[] bottom_top = new int[grid.length];

        int[] left_right = new int[grid[0].length];

        int count=0;
        for(int j= 0; j<grid[0].length;j++){
         int min = Integer.MIN_VALUE;
        for(int i = 0; i < grid.length;i++){

                if(grid[i][j]>= min){
                    min = grid[i][j];
                }

            }

            bottom_top[count++] = min;
        }

        count = 0;
        for(int i= 0; i<grid.length;i++){
         int min = Integer.MIN_VALUE;
        for(int j = 0; j < grid[0].length;j++){

                if(grid[i][j]>= min){
                    min = grid[i][j];
                }

            }

            left_right[count++] = min;
        }

        for(int i = 0; i<grid.length; i++){
        for(int j = 0; j<grid[0].length; j++){

                if(grid[i][j]<GetSmaller(bottom_top[j],left_right[i]))
                sum = sum +GetSmaller(bottom_top[j],left_right[i]) - grid[i][j];
        }

    }

    return sum;
 }

    public int GetSmaller(int a , int b){

        return a>b? b: a;
    }
}  

原文地址:https://www.cnblogs.com/codingyangmao/p/11308117.html

时间: 2024-10-06 00:22:05

(Medium) Max Increase to Keep City SkyLine LeetCode的相关文章

[LeetCode] 807. Max Increase to Keep City Skyline

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 differen

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). Heig

BZOJ1628: [Usaco2007 Demo]City skyline

1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 256  Solved: 210[Submit][Status] Description The best part of the day for Farmer John's cows is when the sun sets. They can see the skyline of the distant city. Bessie w

[USACO2005][POJ3044]City Skyline(贪心+单调栈)

题目:http://poj.org/problem?id=3044 题意:以坐标的形式给出一张图,表示一些楼房的正视图,求出楼房的最少个数. 分析:和小学常做的立方体问题很像,很容易想到一个贪心方法,那就是尽量把矮的楼房放在高的楼房的前面,即连续的等高的一些"X"我们把它视为一座楼房. 具体的做法可以维护一个Y坐标单增的栈,从左到右读入每个坐标,将Y坐标与栈顶坐标比较: 若Y==栈顶坐标,那么接着读下面一个坐标 若Y>栈顶坐标,那么把Y坐标加入栈成为新的栈顶 若Y<栈顶坐标

[Usaco2007 Demo][BZOJ1628] City skyline

1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 320  Solved: 260[Submit][Status][Discuss] Description Input 第一行给出N,W 第二行到第N+1行:每行给出二个整数x,y,输入的x严格递增,并且第一个x总是1 Output 输出一个整数,表示城市中最少包含的建筑物数量 Sample Input 10 26 1 1 2 2 5

bzoj1683[Usaco2005 Nov]City skyline 城市地平线

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1683 Input 第1行:2个用空格隔开的整数N和W. 第2到N+1行:每行包括2个用空格隔开的整数x,y,其意义如题中所述.输入中的x严格递增,并且第一个z总是x. Output 输出一个整数,表示城市中最少包含的建筑物数量. Sample Input 10 26 1 1 2 2 5 1 6 3 8 1 11 0 15 2 17 3 20 2 22 1 INPUT DETAILS: T

BZOJ 1628 [Usaco2007 Demo]City skyline:单调栈

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1628 题意: 题解: 单调栈. 单调性: 栈内元素高度递增. 一旦出现比栈顶小的元素,则表明一栋房子的结束. 入栈: 如果出现了一个新的高度b(栈中没有),则入栈. 表明从现在开始,一定有一栋高度为b的房子,只是我们不知道它在哪里结束而已. 出栈: 对于现在的高度b,将栈中所有高度 > b的元素都出栈. 因为此时比b高的房子不得不结束. 每出栈一个元素,ans++. 注:最后要再多算一次

City Skyline

题目大意:(poj 3044) 给出城市的正视图,所有的大楼都是矩形,给出正视图每个高度改变时的坐标,问最少有多少大楼.一共N个矩形,N<=50000 解题过程: 首先可以把问题转化一下:有N块木板要粉刷,每次只能刷矩形,并且矩形要挨着地面,也不能刷到外面,最少刷多少次. 题目给出的每块木板的宽度其实是木有用的,因为相同高度且挨在一起的木板肯定同时刷.所以可以把所有的宽度缩成1. 那么根据贪心的思想,如果横着刷一次可以刷掉多个矩形,那么肯定横着刷.但是数据范围有点大,想不出什么好的办法,我就用了

[LeetCode] 011. Container With Most Water (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 011.Container_With_Most_Water (Medium) 链接: 题目:https://oj.leetcode.com/problems/container-with-most-water/ 代码(github):https://github.com/illuz/leetcode 题意: 给一些