Merge Intervals

Question

Given a collection of intervals, merge all overlapping intervals.

For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].

Tags

  • Array
  • Sort

Thought

Sort the array by the start in the interval, then add the interval in order.

Code

# 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 intervals == []:
            return []
        sort_interval = sorted(intervals, key=lambda x: x.start)
        result = [sort_interval[0]]
        for i, item in enumerate(sort_interval[1:]):
            if item.start <= result[-1].end:
                result[-1] = Interval(result[-1].start, max(item.end, result[-1].end))
            else:
                result.append(item)
        return result

results matching ""

    No results matching ""