python leetcode练习(807 Max Increase to Keep City Skyline)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python leetcode练习(807 Max Increase to Keep City Skyline)相关的知识,希望对你有一定的参考价值。
class Solution:
def maxIncreaseKeepingSkyline(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
# return skyline from Top or Bottom
def fromTop(grid):
listhori = []
for i in range(len(grid)):
listhori.append(max(grid[i]))
return listhori
# return skyline from Left or Right
def fromLeft(grid):
listveri = []
for i in range(len(grid)):
name = 'list' + str(i)
locals()[name]=[] # use locals() to assign the changable var name
for j in range(len(grid[0])):
locals()[name].append(grid[j][i])
listveri.append(max(locals()[name]))
return listveri
listhori = fromTop(grid)
listveri = fromLeft(grid)
# creat a grid
increase = []
for i in range(len(grid)):
increase.append([])
for j in range(len(grid[0])):
increase[i].append("")
# print (increase) debug info
for i in range(len(grid)):
for j in range(len(grid[0])):
increase[i][j] = min(listhori[i], listveri[j]) - grid[i][j]
# debug info
#print (listhori)
#print (listveri)
#print (increase)
sum_all = 0
for i in range(len(increase)):
sum_all += sum(increase[i])
return sum_all
"""
list fromTop(grid) {
return [grid[0].max, grid[1].max, grid[2].max, grid[3].max]
}
list fromLeft(grid) {
list1 = [grid[0][0], grid[1][0], grid[2][0],grid[3][0]]
list2 = [grid[0][1], grid[1][1], grid[2][1],grid[3][1]]
list3 = [grid[0][2], grid[1][2], grid[2][2],grid[3][2]]
list4 = [grid[0][3], grid[1][3], grid[2][3],grid[3][3]]
return [list1.max, list2.max, list3.max, list4.max]
for element[i][j] in grid {
increase[i][j] = max(listhori(i),listveri(j)) grid[2][1]
}
"""
以上是关于python leetcode练习(807 Max Increase to Keep City Skyline)的主要内容,如果未能解决你的问题,请参考以下文章