-
Notifications
You must be signed in to change notification settings - Fork 90
/
Number of Airplanes in the Sky.py
64 lines (49 loc) · 1.49 KB
/
Number of Airplanes in the Sky.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Given an interval list which are flying and landing time of the flight. How many airplanes are on the sky at most?
Example
For interval list [[1,10],[2,3],[5,8],[4,7]], return 3
Note
If landing and flying happens at the same time, we consider landing should happen at first.
"""
__author__ = 'Daniel'
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
class Solution:
@staticmethod
def cmp(a, b):
"""
:type a: Interval
:type b: Interval
"""
if a.start != b.start:
return a.start-b.start
else:
return a.end-b.end
def countOfAirplanes(self, airplanes):
"""
:param airplanes: a list of Interval
:return: an integer
"""
return self.count_heap(airplanes)
def count_heap(self, intervals):
"""
Heap
:param intervals: a list of Interval
:return: an integer
"""
import heapq
intervals.sort(cmp=Solution.cmp)
heap = []
cnt = 0
for intv in intervals:
# in, with upper boundary
heapq.heappush(heap, intv.end)
# out, by comparing lower boundary
while heap[0] <= intv.start:
heapq.heappop(heap)
cnt = max(cnt, len(heap))
return cnt
if __name__ == "__main__":
assert Solution().countOfAirplanes([Interval(i[0], i[1]) for i in [[1, 10], [2, 3], [5, 8], [4, 7]]]) == 3