Skip to content

Insert Interval

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

intervals is sorted by start and has no overlaps. Insert new_interval into it, merging wherever needed, and return the resulting list (still sorted, still with no overlaps). Touching intervals merge.

intervals = [[1, 3], [6, 9]], new_interval = [2, 5]
  →  [[1, 5], [6, 9]]
 
intervals = [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], new_interval = [4, 8]
  →  [[1, 2], [3, 10], [12, 16]]
 
intervals = [], new_interval = [5, 7]  →  [[5, 7]]

Constraints

  • 0 <= intervals.length <= 10^4
  • 0 <= start <= end <= 10^5 for every interval, including new_interval