0%

807. 保持城市天际线

807. 保持城市天际线

直接遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
int n = grid.size();
vector<int> rowMax(n), colMax(n);
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
{
rowMax[i] = max(rowMax[i], grid[i][j]);
colMax[j] = max(colMax[j], grid[i][j]);
}
}
int ret{ 0 };
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
ret += min(rowMax[i], colMax[j]) - grid[i][j];
return ret;
}
};