/56. Merge Intervals

56. Merge Intervals

Medium
Arrays51.3% acceptance

Given a list of integer pairs representing closed intervals, merge all overlapping intervals and return a list of the resulting non-overlapping intervals sorted by their start values. Each interval is represented as a list [start, end] with start <= end.

Example 1

Input: [[2,5],[1,2],[6,8],[8,10]]

Output: [[1,5],[6,10]]

Explanation: [2,5] and [1,2] overlap and merge to [1,5]. [6,8] and [8,10] overlap and merge to [6,10].

Example 2

Input: [[0,1],[1,3],[4,5]]

Output: [[0,3],[4,5]]

Explanation: [0,1] and [1,3] overlap and merge to [0,3]. [4,5] does not overlap with any other interval.

Example 3

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

Output: [[1,2],[3,4],[5,7]]

Explanation: No intervals overlap, so the result is the sorted intervals.

Constraints

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

Case 1

Input: [[10,12],[11,15],[16,18]]

Expected: [[10,15],[16,18]]

Case 2

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

Expected: [[0,2]]

Case 3

Input: [[5,6],[2,3],[1,2],[3,5]]

Expected: [[1,6]]

Case 4

Input: [[7,8],[8,9],[9,10],[10,11]]

Expected: [[7,11]]

Case 5

Input: [[2,2],[2,2],[2,2]]

Expected: [[2,2]]