Python, LintCode, 391. 数飞机

"""
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())  


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值