807. 保持城市天际线
为保证权益,题目请参考 807. 保持城市天际线(From LeetCode).
解决方案1
Python
python
# 807. 保持城市天际线
# https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/
################################################################################
from typing import List
class Solution:
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
rowMax = [max(grid[i]) for i in range(len(grid))]
colMax = [
max([grid[j][i] for j in range(len(grid))]) for i in range(len(grid[0]))
]
ans = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
ans += min(rowMax[i], colMax[j]) - grid[i][j]
return ans
################################################################################
if __name__ == "__main__":
so = Solution()
print(
so.maxIncreaseKeepingSkyline(
[[3, 0, 8, 4], [2, 4, 5, 7], [9, 2, 6, 3], [0, 3, 1, 0]]
)
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34