/56. Merge Intervals

56. Merge Intervals

Medium
Arrays51.3% acceptance

Given a list of closed intervals, where each interval is represented as a pair of integers [left, right], merge all overlapping intervals and return a list of the resulting non-overlapping intervals, sorted by their start values.

Example 1

Input: [[5,8],[2,4],[3,6],[9,12]]

Output: [[2,8],[9,12]]

Explanation: Intervals [2,4],[3,6],[5,8] overlap and merge to [2,8]. [9,12] is separate.

Example 2

Input: [[0,1],[1,2],[2,3]]

Output: [[0,3]]

Explanation: All intervals are consecutive and overlap, so they merge into [0,3].

Example 3

Input: [[7,9],[1,3],[4,6]]

Output: [[1,3],[4,6],[7,9]]

Explanation: No intervals overlap.

Constraints

  • 1 <= len(interval_list) <= 10_000
  • len(interval_list[i]) == 2 for all i
  • 0 <= interval_list[i][0] <= interval_list[i][1] <= 10_000
Python (current runtime)

Case 1

Input: [[10,15],[12,18],[19,22]]

Expected: [[10,18],[19,22]]

Case 2

Input: [[0,5],[5,10],[10,15]]

Expected: [[0,15]]

Case 3

Input: [[2,2],[3,3],[4,4]]

Expected: [[2,2],[3,3],[4,4]]

Case 4

Input: [[1,5],[2,3],[4,6],[7,8]]

Expected: [[1,6],[7,8]]