Skip to content

Instantly share code, notes, and snippets.

@lealLuo
Last active July 25, 2018 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lealLuo/86b591babbcf8186ddcf58a673458a3d to your computer and use it in GitHub Desktop.
Save lealLuo/86b591babbcf8186ddcf58a673458a3d to your computer and use it in GitHub Desktop.
URL: https://leetcode.com/problems/max-increase-to-keep-city-skyline/description/ direct way to solve it. use too many loops, so the speed is slow
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]
}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment