Blind 75 · Intervals

Insert Interval

Medium

Watch the walkthrough (6:11)

Problem

You have a list of closed intervals, sorted by start and pairwise non-overlapping, and one new interval.
Insert the new interval and merge wherever it overlaps, so the result is again sorted and non-overlapping. Return the resulting list.

Examples

Example 1:
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]

Example 2:
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: [4,8] bridges [3,5], [6,7] and [8,10] into one.

Example 3:
Input: intervals = [], newInterval = [5,7]
Output: [[5,7]]

Example 4:
Input: intervals = [[1,5]], newInterval = [2,3]
Output: [[1,5]]
Explanation: already covered; nothing changes.

Constraints

0 <= intervals.length <= 10^4
intervals is sorted by start; intervals[i] and newInterval are [start, end] with 0 <= start <= end <= 10^5

Prerequisites

Sorting as a setup move — most interval problems are one sort away from a single sweep, and choosing whether to sort by START or by END is the decision that matters.

How to think about it

1. Copy, Merge, Copy the Rest Optimal

Intuition

The intervals are sorted by start and no two of them overlap, so from left to right they fall into three runs: those that end before the new interval starts, those that overlap it, and those that start after it ends. A single pass is enough. Hold the new interval's two ends as start and end. While an interval ends before start, it is copied to the result as it is. While an interval starts at or before end, it overlaps: start becomes the smaller start and end the bigger end. Then the merged interval goes into the result, and the rest is copied. These are closed intervals, so touching counts as overlapping, on either side: the copy check is strictly less than and the merge check is less than or equal. Make the merge check strict and touching intervals no longer merge: on [[2,3],[4,5],[7,8],[9,12],[14,15]] with [6,9], the interval [9,12] starts exactly where the new one ends and is copied instead, so the answer holds [6,9] beside [9,12], two intervals that share 9 and still overlap. Copy what ends before it, merge what overlaps it, then copy the rest.

Algorithm

1. Start an empty result and an index at 0, and hold the new interval's two ends as start and end.
2. While the interval at the index ends before start, copy it to the result and move on.
3. While the interval at the index starts at or before end, it overlaps: set start to the smaller start and end to the bigger end, and move on.
4. Add the merged interval, from start to end, to the result.
5. Copy the rest, and return the result.

Time & Space

Time O(n): each interval is looked at once. Space O(n) for the result, O(1) besides it.

2. Three Phases in One Pass Optimal

Intuition

The input is already sorted and disjoint, which is the gift: everything ending before the new interval starts is untouched, everything overlapping it gets absorbed into one widened interval, and everything after is untouched again. Three phases, one pass, no sorting.

Algorithm

1. Copy intervals that end before the new one starts.
2. While the next interval starts at or before the new one's end, widen the new one — minimum of starts, maximum of ends.
3. Place the widened interval, then copy the rest.

Time & Space

Time O(n). Space O(n) for the output.