Skip to content

Commit 48d11b8

Browse files
committed
数飞机
1 parent 881809c commit 48d11b8

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

number_of_airplanes_in_the_sky.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
# @param airplanes, a list of Interval
5+
# @return an integer
6+
def countOfAirplanes(self, airplanes):
7+
# write your code here
8+
'''
9+
以数据[[1, 10], [2, 3], [5, 8], [4, 7]]为例
10+
1. 字典air_status记录某时刻飞机增加或减少1架,1增加,-1减少。
11+
- a[1], a[2], a[5], a[4] = 1
12+
- a[10], a[3], a[8], a[7] = -1
13+
2. 把全部时刻放在一起排序:[1, 2, 3, 4, 5, 7, 8, 10]
14+
3. 遍历步骤2的结果,根据字典加减飞机数目
15+
'''
16+
ret = 0
17+
air_status = {}
18+
for air in airplanes:
19+
if air.start not in air_status:
20+
air_status[air.start] = 1
21+
else:
22+
air_status[air.start] += 1
23+
if air.end not in air_status:
24+
air_status[air.end] = -1
25+
else:
26+
air_status[air.end] -= 1
27+
time_order = []
28+
for key in air_status: # Python不像C++有set
29+
time_order.append(key)
30+
time_order.sort()
31+
airs = 0
32+
for t in time_order:
33+
airs += air_status[t]
34+
ret = max(airs, ret)
35+
return ret

0 commit comments

Comments
 (0)