"""
Definition of Interval.
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
# @param airplanes, a list of Interval
# @return an integer
def countOfAirplanes(self, airplanes):
# write your code here
count = 0
count_max = 0
time_dict = {}
time_list = []
for i in range(len(airplanes)):
if not airplanes[i].start in time_list:
time_list.append(airplanes[i].start)
if not airplanes[i].end in time_list:
time_list.append(airplanes[i].end)
if airplanes[i].start in time_dict:
time_dict[airplanes[i].start] += 1
else:
time_dict[airplanes[i].start] = 1
if airplanes[i].end in time_dict:
time_dict[airplanes[i].end] -= 1
else:
time_dict[airplanes[i].end] = -1
time_list.sort()
for i in range(len(time_list)):
count += time_dict[time_list[i]]
count_max = max(count, count_max)
return count_max
"""
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 countOfAirplanes(self, airplanes):
# write your code here
if len(airplanes) == 0:
return 0
airplanes.sort(key = lambda interval_tmp: interval_tmp.end)
res = {}
for i in range(len(airplanes)):
keys_tmp = list(res.keys())
for j in range(len(res)):
if not (airplanes[i].start >= keys_tmp[j].end or airplanes[i].end <= keys_tmp[j].start):
tmp_min = max(airplanes[i].start, keys_tmp[j].start)
tmp_max = min(airplanes[i].end, keys_tmp[j].end)
interval_tmp = Interval(tmp_min, tmp_max)
res[interval_tmp] = res[keys_tmp[j]] + 1
del res[keys_tmp[j]]
res[airplanes[i]] = 1
return max(res.values())