前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >43 Max Increase to Keep City Skyline

43 Max Increase to Keep City Skyline

作者头像
devi
发布2021-08-18 16:18:15
发布2021-08-18 16:18:15
51800
代码可运行
举报
文章被收录于专栏:搬砖记录搬砖记录
运行总次数:0
代码可运行

题目

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:

代码语言:javascript
代码运行次数:0
复制
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.

分析

题意:二维平面的每个数字代表楼高(俯视角度),“天际线”就是楼高的轮廓,在不改变天际线的情况下,把所有楼层拔高,求拔高的数值之和

需要点想象力,可以把二维平面想象成棋盘,里面的棋子的高度不同。

思考过后,可以发现,拔高楼层的原则如下: 对于任意一栋楼,本身楼高为a,正视图天际线为b,侧视图天际线为c,拔高条件为:

代码语言:javascript
代码运行次数:0
复制
如果a最大,则跳过
如果a
a小:
	top大,left小,选小
	top小,left大,选小
	==> 选小的
a大:
	则跳过

解答

代码语言:javascript
代码运行次数:0
复制
class Solution {
    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int len = grid.length;
        int[][] tmp = new int[len][len];
        int res=0;
        int[] top = new int[len];
        int[] left = new int[len];
        for(int i=0;i<len;++i){
            int maxLeft = -1;
            int maxTop = -1;
            for(int j=0;j<len;++j){
                // 按行遍历获得侧视图的天际线
                maxLeft = Math.max(grid[i][j], maxLeft);
                // 按列遍历获得正视图的天际线
                maxTop = Math.max(grid[j][i], maxTop);
            }
            // 存放天际线
            left[i] = maxLeft;
            top[i] = maxTop;
        }
        
        for(int i=0;i<len;++i){
            for(int j=0;j<len;++j){
                // 如果a最大,则跳过
                if(tmp[i][j]>left[i] && tmp[i][j]>top[j]) continue;
                // 否则,获取bc中最小的一个,bc分别用行遍历和列遍历
                if (tmp[i][j]<left[i]) tmp[i][j]= Math.min(left[i], top[j]);
                if (tmp[i][j]<top[j]) tmp[i][j]= Math.min(left[i], top[j]);
            }
        }
        // 做差求结果
        for(int i=0;i<len;++i){
            for(int j=0;j<len;++j){
                res+=Math.abs(tmp[i][j] - grid[i][j]);
            }
        }
                
        return res;
    }
}

表现不佳,去瞅瞅看看别人做的。

最后一步可以合并

代码语言:javascript
代码运行次数:0
复制
设res为最终结果,对于任意一栋楼,本身楼高为a,正视图天际线为b,侧视图天际线为c,拔高条件为:
res+=Math.abs(Math.min(b,c)-a)

这种做法的思路跟我上面的一样,但是将两个双层循环合并了。
省略了a最大的情况,是因为Math.abs求绝对值已经暗含了a最大的情况。
代码语言:javascript
代码运行次数:0
复制
class Solution {
    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int len = grid.length;
        int[][] tmp = new int[len][len];
        int res=0;
        int[] top = new int[len];
        int[] left = new int[len];
        for(int i=0;i<len;++i){
            int maxLeft = -1;
            int maxTop = -1;
            for(int j=0;j<len;++j){
                // 按行遍历获得侧视图的天际线
                maxLeft = Math.max(grid[i][j], maxLeft);
                // 按列遍历获得正视图的天际线
                maxTop = Math.max(grid[j][i], maxTop);
            }
            // 存放天际线
            left[i] = maxLeft;
            top[i] = maxTop;
        }
        
        for(int i=0;i<len;++i){
            for(int j=0;j<len;++j){
                res+=Math.abs(grid[i][j]-Math.min(top[j],left[i]));
            }
        }       
        return res;
    }
}

只是减少了一个双层for循环,复杂度依旧是O(n²),速度并没有加快。

由于是云端运算结果,相同的代码的运行速度会有波动。

小结

本题的关键在于对二维数组的行遍历和列遍历。 两层循环,外层i为行,内层j为列; 如果是arr [i] [j],那么就是按行逐个遍历; 如果是arr [j] [i],那么就是按列逐个遍历;

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/03/07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 分析
  • 解答
  • 小结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档