forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskyline.py
More file actions
49 lines (39 loc) · 1.32 KB
/
skyline.py
File metadata and controls
49 lines (39 loc) · 1.32 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
"""
Skyline Problem
Given building triplets [left, right, height], compute the skyline
contour as a list of key points using a heap-based sweep line approach.
Reference: https://leetcode.com/problems/the-skyline-problem/
Complexity:
Time: O(n log n)
Space: O(n)
"""
from __future__ import annotations
import heapq
def get_skyline(lrh: list[list[int]]) -> list[list[int]]:
"""Compute the skyline from a list of buildings.
Args:
lrh: List of [left, right, height] building triplets, sorted
by left coordinate.
Returns:
List of [x, height] key points defining the skyline.
Examples:
>>> get_skyline([[2, 9, 10], [3, 7, 15]])
[[2, 10], [3, 15], [7, 10], [9, 0]]
"""
skyline: list[list[int]] = []
live: list[list[int]] = []
i, n = 0, len(lrh)
while i < n or live:
if not live or i < n and lrh[i][0] <= -live[0][1]:
x = lrh[i][0]
while i < n and lrh[i][0] == x:
heapq.heappush(live, [-lrh[i][2], -lrh[i][1]])
i += 1
else:
x = -live[0][1]
while live and -live[0][1] <= x:
heapq.heappop(live)
height = len(live) and -live[0][0]
if not skyline or height != skyline[-1][1]:
skyline += [[x, height]]
return skyline