MaxIncreaseToKeepCitySkyline [source code]
public class MaxIncreaseToKeepCitySkyline {
static
/******************************************************************************/
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
if (grid.length == 0 || grid[0].length == 0)
return 0;
int sum = 0, R = grid.length, C = grid[0].length;
int[] skyline0 = new int[R], skyline1 = new int[C];
for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) {
skyline0[i] = Math.max (skyline0[i], grid[i][j]);
skyline1[j] = Math.max (skyline1[j], grid[i][j]);
}
for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) {
sum += Math.min (skyline0[i], skyline1[j]) - grid[i][j];
}
return sum;
}
}
/******************************************************************************/
public static void main(String[] args) {
MaxIncreaseToKeepCitySkyline.Solution tester = new MaxIncreaseToKeepCitySkyline.Solution();
int[][][] inputs = {
{{3,0,8,4},{2,4,5,7},{9,2,6,3},{0,3,1,0}}, {{35}},
};
for (int i = 0; i < inputs.length / 2; i++) {
System.out.println (Printer.separator ());
int[][] grid = inputs[2 * i];
int ans = inputs[2 * i + 1][0][0], output = tester.maxIncreaseKeepingSkyline (grid);
System.out.printf ("%s%s, expected: %s\n",
Matrix.printMatrix (grid), Printer.wrap (output + "", output == ans ? 92 : 91), ans
);
}
}
}
二维的比较难想, 先思考一维的skyline是什么样的? 简单升级?
同一个axis上面的两个方向的skyline是相同的: 这个我还想了一会儿才想到, 实际上这个题目给的例子就暴露了这个提示: 只维护了两个skyline;
看懂题目之后有两个思考方向: 好像有数学解法? 也好像只能用一种搜索的解法去做?
有一个简单的想法, 写写看;
UNFINISHED
uwi:
class Solution {
public int maxIncreaseKeepingSkyline(int[][] a) {
int n = a.length, m = a[0].length;
int[] mr = new int[n];
for(int i = 0;i < n;i++){
int max = 0;
for(int j = 0;j < m;j++){
max = Math.max(max, a[i][j]);
}
mr[i] = max;
}
int[] mc = new int[m];
for(int i = 0;i < m;i++){
int max = 0;
for(int j = 0;j < n;j++){
max = Math.max(max, a[j][i]);
}
mc[i] = max;
}
int ans = 0;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
ans += Math.min(mr[i], mc[j]) - a[i][j];
}
}
return ans;
}
}
Problem 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.
Difficulty:Medium
Total Accepted:5.5K
Total Submissions:6.7K
Contributor:awice