Skip to content

Commit 7a05a17

Browse files
Merge pull request #2 from gantavya12/patch-2
Create The-Skyline-Problem.py
2 parents 50d7979 + 3bd78b7 commit 7a05a17

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Python/The-Skyline-Problem.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def getSkyline(self, buildings):
3+
points = [(l,h,-1,i) for i, (l,r,h) in enumerate(buildings)]
4+
points += [(r,h,1,i) for i, (l,r,h) in enumerate(buildings)]
5+
points.sort(key = lambda x: (x[0], x[1]*x[2]))
6+
heap, active, ans = [(0,-1)], set([-1]), []
7+
8+
for x, h, lr, ind in points:
9+
if lr == -1: active.add(ind)
10+
else: active.remove(ind)
11+
12+
if lr == -1:
13+
if h > -heap[0][0]:
14+
ans.append([x, h])
15+
heappush(heap, (-h, ind))
16+
else:
17+
if h == -heap[0][0]:
18+
while heap and heap[0][1] not in active: heappop(heap)
19+
if -heap[0][0] != ans[-1][1]:
20+
ans.append([x, -heap[0][0]])
21+
22+
return ans

0 commit comments

Comments
 (0)