Merge Intervals

Difficulty: Medium

You're given an unsorted list of intervals, each a start and end pair. Some overlap: they share at least one point. Merge every group of overlapping intervals into one interval that spans them all, and return the result.

Intervals that just touch (one ends where the other begins) count as overlapping, since they share that point.

Examples

Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]

[1,3] and [2,6] share the range 2-3, so they merge into [1,6]. The other two don't touch anything.

Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]

[1,4] and [4,5] touch at the point 4, so they merge into [1,5].

Input: intervals = [[1,4],[2,3]]
Output: [[1,4]]

[2,3] is completely contained inside [1,4], so it disappears into it.

Constraints

  • 1 <= intervals.length <= 10^4

  • intervals[i].length == 2

  • 0 <= start <= end <= 10^4

Approach

The direct way: repeatedly find two overlapping intervals, merge them, and start over, until nothing overlaps. It's correct but slow, because it keeps re-checking pairs that will never overlap.

If you first sort by start time, an interval can only overlap the one right before it. That makes it a single scan: keep a "current merged interval." If the next interval starts inside it, stretch it to cover that interval. Otherwise, close it and start a new one.

Solutions

Brute Force — Repeated Pairwise Merging

Scan for any overlapping pair. Merge it into one interval, replacing the two originals, and start scanning again from the top. Stop when a full pass finds nothing to merge.

function merge(intervals) {
  const result = intervals.map((iv) => [...iv]);
  let mergedSomething = true;

  while (mergedSomething) {
    mergedSomething = false;
    for (let i = 0; i < result.length && !mergedSomething; i++) {
      for (let j = i + 1; j < result.length; j++) {
        const overlaps = result[i][0] <= result[j][1] && result[j][0] <= result[i][1];
        if (overlaps) {
          result[i][0] = Math.min(result[i][0], result[j][0]);
          result[i][1] = Math.max(result[i][1], result[j][1]);
          result.splice(j, 1);
          mergedSomething = true;
          break;
        }
      }
    }
  }

  return result;
}

Time: O(n^3) in the worst case — each of up to n merge passes rescans all O(n^2) pairs · Space: O(n) for the working copy

Optimal — Sort by Start, Then Scan

Sort by start, then walk left to right. If the next interval starts at or before the last result's end, they overlap (or touch), so stretch the last result's end to cover it. Otherwise it can't reach anything already merged, so it starts a new entry.

function merge(intervals) {
  const sorted = [...intervals].sort((a, b) => a[0] - b[0]);
  const result = [sorted[0]];

  for (let i = 1; i < sorted.length; i++) {
    const last = result[result.length - 1];
    const current = sorted[i];

    if (current[0] <= last[1]) {
      last[1] = Math.max(last[1], current[1]);
    } else {
      result.push(current);
    }
  }

  return result;
}

Time: O(n log n) — dominated by the sort, the scan itself is O(n) · Space: O(n) for the sorted copy and result