Python 98% straight forward

  • 0

    sort then iterate while keeping min and max. If no overlap, add previous min,max as an interval, then move pivot pointer over to start a new min,max run

        def merge(self, intervals):
            :type intervals: List[Interval]
            :rtype: List[Interval]
            if not intervals: return []
            #self.mergeSort(intervals, 0, len(intervals)-1)
            intervals = sorted(intervals, key=lambda i: i.start)
            rList = []
            i = j = 0
            while j < len(intervals):
                mn, mx = intervals[i].start, intervals[i].end
                while j < len(intervals) and intervals[j].start <= mx:
                    if intervals[j].end > mx: mx = intervals[j].end
                    j += 1
                rList.append(Interval(mn, mx))
                i = j
            return rList

Log in to reply

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