Created
February 22, 2021 20:50
-
-
Save mvallebr/6ddf97c9e3000bc21ca523411232a96d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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