The best python solution so far (beats 100% python submissions, 09/02/16)


  • 0
    I
    # Definition for an interval.
    # class Interval(object):
    #     def __init__(self, s=0, e=0):
    #         self.start = s
    #         self.end = e
    
    class Solution(object):
        def merge(self, intervals):
            """
            :type intervals: List[Interval]
            :rtype: List[Interval]
            """
            if not intervals:
                return intervals
            intervals = sorted(intervals, key=lambda x: x.start)
            answer = [intervals[0]]
            for interval in intervals[1:]:
                if interval.start <= answer[-1].end:
                    if interval.end > answer[-1].end:
                        answer[-1].end = interval.end
                else:
                    answer.append(interval)
            return answer
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.