题目链接:
力扣https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/
【分析】这道题好像没有中等难度,求出每行每列的最大值,然后每个元素和行列里最大值的最小值比较即可。
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
int i, j, n = grid.length, res = 0;
int[] row = new int[n];
int[] col = new int[n];
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
row[i] = Math.max(row[i], grid[i][j]);
col[j] = Math.max(col[j], grid[i][j]);
}
}
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
res += Math.max(0, Math.min(row[i], col[j]) - grid[i][j]);
}
}
return res;
}
}