-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path391.number-of-airplanes-in-the-sky.py
65 lines (59 loc) · 1.58 KB
/
391.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
65
# Tag: Sweep Line
# Time: O(NlogN)
# Space: O(N)
# Ref: -
# Note: -
# Given an list `interval`, which are taking off and landing time of the flight.
# How many airplanes are there at most at the same time in the sky?
#
# **Example 1:**
#
# ```
# Input: [(1, 10), (2, 3), (5, 8), (4, 7)]
# Output: 3
# Explanation:
# The first airplane takes off at 1 and lands at 10.
# The second ariplane takes off at 2 and lands at 3.
# The third ariplane takes off at 5 and lands at 8.
# The forth ariplane takes off at 4 and lands at 7.
# During 5 to 6, there are three airplanes in the sky.
# ```
#
# **Example 2:**
#
# ```
# Input: [(1, 2), (2, 3), (3, 4)]
# Output: 1
# Explanation: Landing happen before taking off.
# ```
#
# If landing and taking off of different planes happen at the same time, we consider landing should happen at first.
from typing import (
List,
)
from lintcode import (
Interval,
)
"""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
"""
@param airplanes: An interval array
@return: Count of airplanes are in the sky.
"""
def count_of_airplanes(self, airplanes: List[Interval]) -> int:
# write your code here
times = []
for plane in airplanes:
times.append([plane.start, 1])
times.append([plane.end, -1])
count, result = 0, 0
for _, delta in sorted(times):
count += delta
result = max(result, count)
return result