A python solution beat 98%


  • 0
    S
    class Solution(object):
        def merge(self, intervals):
            """
            :type intervals: List[Interval]
            :rtype: List[Interval]
            """
            if intervals == []:
                return []
            sorted_interval = sorted(intervals, key=lambda x: x.start)
            sorted_interval.append(Interval(2**31, 2**31))
            res = []
            flag = False
            for i in range(len(sorted_interval)-1):
                if not flag:
                    start = sorted_interval[i].start
                    end = sorted_interval[i].end
                    flag = True
                if end < sorted_interval[i+1].start:
                    res.append(Interval(start, end))
                    flag = False
                else:
                    end = max(end, sorted_interval[i+1].end)
            return res
                
    

Log in to reply
 

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