ntree.ai
ntree.aiMerge Intervals

Merge Intervals

mediumsolvedattempted

Given an array of intervals where intervals[i] = [start, end], return a new array with overlapping intervals merged.

Intervals that touch at a point (e.g. [1, 4] and [4, 5]) count as overlapping.

The answer may be returned in any order.

Example 1:

[2, 6]
[15, 18]
[1, 3]
[8, 10]
merged
05101520
Input:  intervals = [[2, 6], [15, 18], [1, 3], [8, 10]]
Output: [[1, 6], [8, 10], [15, 18]]
Explanation: [1, 3] and [2, 6] overlap, so they become [1, 6].

Example 2:

[1, 4]
[4, 5]
merged
12345
Input:  intervals = [[1, 4], [4, 5]]
Output: [[1, 5]]
Explanation: they meet at 4, which counts as overlapping.

Constraints:

  • 1 <= length of intervals <= 10^5
  • length of intervals[i] == 2
  • -10^9 <= start <= end <= 10^9
def merge(intervals: list[list[int]]) -> list[list[int]]:
pass
 
[2, 6]
[15, 18]
[1, 3]
[8, 10]
05101520

Press Visualise to step through your code.