这题medium有点说不过去,起码也得是个easy吧,直接上题解
class Solution:
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
if not grid or not grid[0]:
return 0
row = [0] * len(grid)
col = [0] * len(grid[0])
# 找到每一行的最大值
for i in range(len(grid)):
row[i] = max(grid[i])
# 找到每一列的最大值
for j in range(len(grid[0])):
max_col = 0
for i in range(len(grid)):
if grid[i][j] > max_col:
max_col = grid[i][j]
col[j] = max_col
new_grid = copy.deepcopy(grid)
sum_add = 0
# 每一个元素对应的新值为该元素所在行和列中的较小值,即min(row[i], col[j])
for i in range(len(grid)):
for j in range(len(grid[0])):
new_grid[i][j] = min(row[i], col[j])
sum_add += new_grid[i][j] - grid[i][j]
return sum_add
下面是官方题解,没有对比就没有伤害
class Solution:
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
rowMax = list(map(max, grid))
colMax = list(map(max, zip(*grid)))
return sum(min(rowMax[i], colMax[j]) - h for i, row in enumerate(grid) for j, h in enumerate(row))
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/solution/bao-chi-cheng-shi-tian-ji-xian-by-leetco-n2lu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。