From 3bd78b7a1503b80057667cd7ff5e8b35181eff2e Mon Sep 17 00:00:00 2001 From: gantavya12 <91743053+gantavya12@users.noreply.github.com> Date: Sat, 1 Oct 2022 02:40:39 +0530 Subject: [PATCH] Create The-Skyline-Problem.py --- Python/The-Skyline-Problem.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Python/The-Skyline-Problem.py diff --git a/Python/The-Skyline-Problem.py b/Python/The-Skyline-Problem.py new file mode 100644 index 00000000..157519ef --- /dev/null +++ b/Python/The-Skyline-Problem.py @@ -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