Skip to content

Commit

Permalink
Create The-Skyline-Problem.py
Browse files Browse the repository at this point in the history
  • Loading branch information
gantavya12 authored Sep 30, 2022
1 parent 50d7979 commit 3bd78b7
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Python/The-Skyline-Problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def getSkyline(self, buildings):
points = [(l,h,-1,i) for i, (l,r,h) in enumerate(buildings)]
points += [(r,h,1,i) for i, (l,r,h) in enumerate(buildings)]
points.sort(key = lambda x: (x[0], x[1]*x[2]))
heap, active, ans = [(0,-1)], set([-1]), []

for x, h, lr, ind in points:
if lr == -1: active.add(ind)
else: active.remove(ind)

if lr == -1:
if h > -heap[0][0]:
ans.append([x, h])
heappush(heap, (-h, ind))
else:
if h == -heap[0][0]:
while heap and heap[0][1] not in active: heappop(heap)
if -heap[0][0] != ans[-1][1]:
ans.append([x, -heap[0][0]])

return ans

0 comments on commit 3bd78b7

Please sign in to comment.