Leetcode: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). 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.

英语不怎么好,没读懂题目,谷歌翻译之后才知道的是什么意思。虽然是个Medium的题,但是输入的范围给的已经很精确了,所以机智的我就一次AC了

来,让你瞧瞧我的代码[黑脸]!

 1 class Solution {
 2     public int maxIncreaseKeepingSkyline(int[][] grid) {
 3         int rownum = grid.length;
 4         int colnum = grid[0].length;
 5         int[] top = new int[colnum];
 6         int[] right = new int[rownum];
 7         for(int i = 0 ; i < rownum ; i++) //行遍历
 8         {
 9             int max = 0;
10             for(int j = 0 ; j < colnum ; j++)
11             {
12                 if(grid[i][j] > max)
13                     max = grid[i][j];
14             }
15             right[i] = max;
16         }
17     
18         for(int i = 0 ; i < colnum ; i++)//列遍历
19         {
20             int max = 0;
21             for(int j = 0 ; j < rownum ; j++)
22             {
23                 if(grid[j][i] > max)
24                     max = grid[j][i];
25             }
26             top[i] = max;
27         }
28  
29         int newGrid[][] = new int[rownum][colnum];
30         for(int i = 0 ; i < rownum ; i++)
31         {
32             for(int j = 0 ; j < colnum ; j++)
33             {
34                 int min = 0;
35                 if(right[i] < top [j])
36                     min = right[i];
37                 else
38                     min = top[j];
39                 newGrid[i][j] = min;
40             }
41         }
42         int sum = 0;
43         for(int i = 0 ; i < rownum ; i++)
44             for(int j = 0 ; j < colnum ; j++)
45                 sum = sum + (newGrid[i][j] - grid[i][j]);
46         return sum;
47     }
48 }

就是先每行遍历一遍,把每行中的最大值都保存在right里,每列中的最大值都保存在top里,最后将grid遍历一遍,把grid中每一个量都改为top与right中的较小值,然后再跟原来的grid比较,计算差值,运行时间是14ms

再来欣赏一下优秀代码---7ms的

 1 class Solution {
 2     public int maxIncreaseKeepingSkyline(int[][] grid) {
 3         int M = grid.length;
 4         int N = grid[0].length;
 5         int[] topBottomMax = new int[N];
 6         int[] leftRightMax = new int[M];
 7         
 8         for(int i = 0; i < M; i++) {
 9             for (int j = 0; j < N; j++) {
10                 leftRightMax[i] = Math.max(grid[i][j], leftRightMax[i]);
11                 topBottomMax[j] = Math.max(grid[i][j], topBottomMax[j]);
12             }
13         }
14         
15         int sum = 0;
16         for (int i = 0; i < M; i++) {
17             for (int j = 0; j < N; j++) {
18                 int diff = Math.min(leftRightMax[i], topBottomMax[j]) - grid[i][j];
19                 sum += diff > 0 ? diff : 0;
20             }
21         }
22         
23         return sum;
24     }
25 }

比我的精简了一点,但基本方法都一样。。。把好几个循环都写到了一起Emmm非常值得你去学习!!

 

posted @ 2018-09-11 20:08  kyo酱  阅读(102)  评论(0编辑  收藏  举报