方法一:贪心。
这道题的思想就是找出每行、每列的最大值,然后取两者的最小值作为每个单元格上的值,求差
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
int rows = grid.length;
if (rows == 0) return 0;
int cols = grid[0].length;
int[] linemax = new int[rows];
int[] colmax = new int[cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
linemax[i] = Math.max(linemax[i], grid[i][j]);
colmax[j] = Math.max(colmax[j], grid[i][j]);
}
}
int res = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
res += (Math.min(linemax[i], colmax[j]) - grid[i][j]);
grid[i][j] = Math.min(linemax[i], colmax[j]);
}
}
return res;
}
}