Skip to content

Commit 4bbca01

Browse files
218: The Skyline Problem
1 parent 89a75cf commit 4bbca01

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

python/the_skyline_problem.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import bisect
2+
3+
class Solution:
4+
def getSkyline(self, buildings):
5+
h = []
6+
for b in buildings:
7+
h.append((b[0], -b[2]))
8+
h.append((b[1], b[2]))
9+
10+
h.sort()
11+
prev = cur = 0
12+
m = [0]
13+
res = []
14+
15+
for i in h:
16+
if i[1] < 0:
17+
bisect.insort(m, -i[1])
18+
else:
19+
m.remove(i[1])
20+
cur = max(m)
21+
if cur != prev:
22+
res.append([i[0], cur])
23+
prev = cur
24+
25+
return res

0 commit comments

Comments
 (0)