示例 1:
示例 2:
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
int n = grid.length;
int res = 0;
int[] rawMax = new int[n];
int[] colMax = new int[n];
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
rawMax[i] = Math.max(rawMax[i],grid[i][j]);
colMax[j] = Math.max(colMax[j],grid[i][j]);
}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
res += Math.min((rawMax[i] - grid[i][j]),(colMax[j] - grid[i][j]));
}
}
return res;
}
}
int maxIncreaseKeepingSkyline(int** grid, int gridSize, int* gridColSize)
{
int n = gridSize;
int res = 0;
int* rawMax = (int*)calloc(n, sizeof(int));
int* colMax = (int*)calloc(n, sizeof(int));
for(int i = 0;i < n;i++)
{
for(int j = 0;j < n;j++)
{
rawMax[i] = fmax(rawMax[i], grid[i][j]);
colMax[j] = fmax(colMax[j], grid[i][j]);
}
}
for(int i = 0;i < n;i++)
{
for(int j = 0;j < n;j++)
{
res += fmin(rawMax[i] - grid[i][j], colMax[j] - grid[i][j]);
}
}
return res;
}
class Solution:
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
n = len(grid)
res = 0
rawMax = [0] * n
colMax = [0] * n
for i in range(0, n):
for j in range(0, n):
rawMax[i] = max(rawMax[i],grid[i][j])
colMax[j] = max(colMax[j],grid[i][j])
for i in range(0, n):
for j in range(0, n):
res += min(rawMax[i] - grid[i][j], colMax[j] - grid[i][j])
return res
class Solution {
public:
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
int n = grid.size();
int res = 0;
vector<int> rawMax(n);
vector<int> colMax(n);
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
rawMax[i] = fmax(rawMax[i],grid[i][j]);
colMax[j] = fmax(colMax[j],grid[i][j]);
}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
res += fmin(rawMax[i] - grid[i][j],colMax[j] - grid[i][j]);
}
}
return res;
}
};