Skip to content

Merge Intervals

MediumIntervals
Decode it first. Clues, constraints, pattern and plan before any code.

Given a list of intervals [start, end], merge every group that overlaps and return the result sorted by start. Intervals that touch (one ends where the next starts) count as overlapping.

[[1, 3], [2, 6], [8, 10], [15, 18]]  →  [[1, 6], [8, 10], [15, 18]]
[[1, 4], [4, 5]]                     →  [[1, 5]]
[[4, 7], [1, 4]]                     →  [[1, 7]]   (input may be unsorted)

Constraints

  • 0 <= intervals.length <= 10^4
  • 0 <= start <= end <= 10^4