Skip to content
LC-0056 Medium LeetCode

56. Merge Intervals

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 49% Topics: Array, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn)
# Space: O(1)

class Solution(object):
    def merge(self, intervals):
        """
        :type intervals: List[List[int]]
        :rtype: List[List[int]]
        """
        intervals.sort()
        result = []
        for interval in intervals:
            if not result or interval[0] > result[-1][1]:
                result.append(interval)
            else:
                result[-1][1] = max(result[-1][1], interval[1])
        return result

Solution from kamyu104/LeetCode-Solutions · MIT