-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path218_the_skyline_problem.py
More file actions
77 lines (56 loc) · 1.61 KB
/
218_the_skyline_problem.py
File metadata and controls
77 lines (56 loc) · 1.61 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
REF: https://briangordon.github.io/2014/08/the-skyline-problem.html
"""
import heapq
class HashHeapq:
def __init__(self):
self.heap = []
self.deleted = {}
def push(self, val):
heapq.heappush(self.heap, val)
def pop(self):
if self.is_empty():
return -1
return heapq.heappop(self.heap)
def remove(self, val):
if self.is_empty():
return
if val not in self.deleted:
self.deleted[val] = 0
self.deleted[val] += 1
def top(self):
if self.is_empty():
return -1
return self.heap[0]
def is_empty(self):
while self.heap and self.deleted.get(self.heap[0]):
val = heapq.heappop(self.heap)
self.deleted[val] -= 1
return not self.heap
class Solution:
def getSkyline(self, buildings):
"""
:type buildings: List[List[int]]
:rtype: List[List[int]]
"""
ans = []
if not buildings:
return ans
time = []
for x, _x, height in buildings:
time.append((x, height, True))
time.append((_x, height, False))
time.sort()
heap = HashHeapq()
for x, height, is_start in time:
if is_start:
heap.push(-height)
else:
heap.remove(-height)
max_h = -heap.top() if not heap.is_empty() else 0
if ans and ans[-1][0] == x:
ans.pop()
if ans and ans[-1][1] == max_h:
continue
ans.append([x, max_h])
return ans