Skip to content

Instantly share code, notes, and snippets.

@mvallebr
Created February 22, 2021 20:50
Show Gist options
  • Select an option

  • Save mvallebr/6ddf97c9e3000bc21ca523411232a96d to your computer and use it in GitHub Desktop.

Select an option

Save mvallebr/6ddf97c9e3000bc21ca523411232a96d to your computer and use it in GitHub Desktop.
class Solution:
def getSkyline(self, buildings: List[List[int]]) -> List[List[int]]:
def divide_conquer(i, j):
if j < i:
return []
elif j == i:
x1, x2, h = buildings[i]
return [[x1, h], [x2, 0]]
else:
middle = (i + j) // 2
part1 = divide_conquer(i, middle)
part2 = divide_conquer(middle + 1, j)
# merge
i, j = 0, 0
y1, y2 = 0, 0
result = []
old_y = None
while i < len(part1) and j < len(part2):
xi, yi = part1[i]
xj, yj = part2[j]
if xi <= xj:
new_x = xi
y1 = yi
i += 1
if xj <= xi:
new_x = xj
y2 = yj
j += 1
new_y = max(y1, y2)
if old_y != new_y:
result.append([new_x, new_y])
old_y = new_y
return result + part1[i:] + part2[j:]
return divide_conquer(0, len(buildings) - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment