题目
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.
分析
题意:二维平面的每个数字代表楼高(俯视角度),“天际线”就是楼高的轮廓,在不改变天际线的情况下,把所有楼层拔高,求拔高的数值之和
需要点想象力,可以把二维平面想象成棋盘,里面的棋子的高度不同。
思考过后,可以发现,拔高楼层的原则如下:
对于任意一栋楼,本身楼高为a,正视图天际线为b,侧视图天际线为c,拔高条件为:
如果a最大,则跳过
如果a
a小:
top大,left小,选小
top小,left大,选小
==> 选小的
a大:
则跳过
解答
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;
}
}
表现不佳,去瞅瞅看看别人做的。
最后一步可以合并
设res为最终结果,对于任意一栋楼,本身楼高为a,正视图天际线为b,侧视图天际线为c,拔高条件为:
res+=Math.abs(Math.min(b,c)-a)
这种做法的思路跟我上面的一样,但是将两个双层循环合并了。
省略了a最大的情况,是因为Math.abs求绝对值已经暗含了a最大的情况。
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],那么就是按列逐个遍历;
来源:CSDN
作者:舜绪
链接:https://blog.csdn.net/weixin_43367550/article/details/104712307