-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path0218-the-skyline-problem.py
50 lines (38 loc) · 1.44 KB
/
0218-the-skyline-problem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# time complexity: O(nlogn)
# space complexity: O(1)
from typing import List
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, num):
if num != self.parent[num]:
self.parent[num] = self.find(self.parent[num])
return self.parent[num]
def union(self, x, y):
self.parent[x] = self.parent[y]
class Solution:
def getSkyline(self, buildings: List[List[int]]) -> List[List[int]]:
coordinates = sorted(
list(set([x for building in buildings for x in building[:2]])))
n = len(coordinates)
heights = [0] * n
indexMap = {x: idx for idx, x in enumerate(coordinates)}
buildings.sort(key=lambda x: -x[2])
skyline = []
uf = UnionFind(n)
for leftX, rightX, height in buildings:
left, right = indexMap[leftX], indexMap[rightX]
while left < right:
left = uf.find(left)
if left < right:
uf.union(left, right)
heights[left] = height
left += 1
for i in range(n):
if i == 0 or heights[i] != heights[i - 1]:
skyline.append([coordinates[i], heights[i]])
return skyline
buildings = [[2, 9, 10], [3, 7, 15], [5, 12, 12], [15, 20, 10], [19, 24, 8]]
print(Solution().getSkyline(buildings))
buildings = [[0, 2, 3], [2, 5, 3]]
print(Solution().getSkyline(buildings))