Simple O(nlog(n)) python solution


  • 0
    Z
    class Solution(object):
        def merge(self, intervals):
            def add_and_merge(interval, merged_intervals):
                if not merged_intervals:
                    merged_intervals.append(interval)
                else:
                    last_interval = merged_intervals[-1]
                    if last_interval.end < interval.start:
                        merged_intervals.append(interval)
                    else:
                        last_interval.end = max(last_interval.end, 
                            interval.end)
            intervals.sort(key=lambda i: i.start)
            merged_intervals = []
            for interval in intervals:
                add_and_merge(interval, merged_intervals)
            return merged_intervals

Log in to reply
 

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