-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0218-the-skyline-problem.py
More file actions
67 lines (50 loc) · 1.92 KB
/
0218-the-skyline-problem.py
File metadata and controls
67 lines (50 loc) · 1.92 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
# https://leetcode.com/problems/the-skyline-problem/submissions/
from heapq import *
class Solution:
def getSkyline(self, buildings: List[List[int]]) -> List[List[int]]:
points = []
for x1,x2,y in buildings:
points.append([x1,y,'s'])
points.append([x2,y,'e'])
self.heights = []
self.inHeap = {} # height: inHeap, shouldBeInHeap
self.pushHeap(0)
points.sort(key=lambda x:x[2],reverse=True)
points.sort(key=lambda x:-x[1])
points.sort(key=lambda x:x[0])
mainPoints = []
for x,y,p in points:
if p == 's':
if y > self.getMaxHeap():
mainPoints.append([x,y])
self.pushHeap(y)
elif p == 'e':
self.inHeap[y][1]-=1
if y > self.getMaxHeap():
mainPoints.append([x,self.getMaxHeap()])
output = []
for x,y in mainPoints:
if output and (output[-1][0] == x or output[-1][1] == y):
output.pop()
output.append([x,y])
return output
def getMaxHeap(self):
val = -self.heights[0]
while self.inHeap[val][0] != self.inHeap[val][1]:
self.removeHeap(val)
val = -self.heights[0]
return val
def pushHeap(self, val):
if val in self.inHeap and self.inHeap[val][1] < self.inHeap[val][0]:
self.inHeap[val][1] += 1
return
elif val in self.inHeap:
self.inHeap[val][0]+=1
self.inHeap[val][1]+=1
else:
self.inHeap[val] = [1,1]
heappush(self.heights,-val)
def removeHeap(self, val):
if self.heights[0] == -val:
self.inHeap[val][0] -= 1
heappop(self.heights)