The problem. You are given an array of intervals, where intervals[i] = [start, end]. Merge all overlapping intervals and return the non-overlapping intervals that cover exactly the same ranges.
Input: [[1,3], [2,6], [8,10], [15,18]]
Output: [[1,6], [8,10], [15,18]] [1,3] and [2,6] overlap, so they become [1,6]
Input: [[1,4], [4,5]]
Output: [[1,5]] touching intervals count as overlappingTwo intervals overlap when one starts before (or exactly when) the other ends. Merging them gives an interval from the earlier start to the later end. Think of meetings in a calendar: back-to-back or overlapping meetings merge into one busy block.
The input isn't sorted — the classic test case lists [15, 18] before [2, 6] — and that's what makes it feel hard at first.
Look for any two intervals that overlap, replace them with their merge, and start looking again. When a full search finds no overlapping pair, every interval is separate.
function merge(intervals) { const list = intervals.map(([s, e]) => [s, e]); let changed = true; while (changed) { // repeat until no two intervals overlap changed = false; for (let i = 0; i < list.length && !changed; i++) { for (let j = i + 1; j < list.length && !changed; j++) { const [a, b] = [list[i], list[j]]; if (a[0] <= b[1] && b[0] <= a[1]) { // they overlap list[i] = [Math.min(a[0], b[0]), Math.max(a[1], b[1])]; list.splice(j, 1); changed = true; } } } } return list.sort((a, b) => a[0] - b[0]);}Time O(n³), space O(n). Each merge removes one interval, there can be n − 1 merges, and finding each one can take O(n²) comparisons.
Sort the intervals by their start. Now any interval that could overlap the one you're building must come right after it: once an interval starts after the current end, every later interval starts even later, so none of them can reach back.
So after sorting, one pass is enough. Keep the last merged interval; for each next interval, either it overlaps (extend the end) or there's a gap (the last interval is final — start a new one).
Why `max` is essential. The obvious-looking last.end = current.end is wrong, and it fails on nested intervals. Take [[1, 10], [2, 3]]: they overlap, but [2, 3] sits entirely inside [1, 10], so the merged end must stay 10 — not shrink to 3.
function merge(intervals) { intervals.sort((a, b) => a[0] - b[0]); // 1. sort by start const merged = [[...intervals[0]]]; for (let i = 1; i < intervals.length; i++) { const last = merged[merged.length - 1]; const [start, end] = intervals[i]; if (start <= last[1]) { last[1] = Math.max(last[1], end); // overlap: extend } else { merged.push([start, end]); // gap: start a new one } } return merged;}Time O(n log n), space O(n). The sort dominates; the pass itself is O(n). The merged list is a second copy of the data — but notice it never holds more intervals than we've already read.
Since the merged intervals never outnumber the ones already read, they can be written back into the input array. read walks forward; write points at the interval currently being built. Overlap? Stretch intervals[write]. Gap? Move write forward and copy the new interval there. At the end, cut the array after write. It's the same reader/writer idea as Move Zeroes.
Enter [start,end] pairs in any order — the first step sorts them. Try the Nested example to watch max keep the end at 10.
Time O(n log n), space O(1) extra beyond the sort — the answer lives in the input array. In Java, Integer.compare(a[0], b[0]) is safer than a[0] - b[0], which can overflow; in C++, sorting vectors compares them element by element, which sorts by start first.
O(n³)Up to n − 1 merges, and finding each overlapping pair can take O(n²) comparisons.
O(n)A working copy of the intervals.
Nothing can beat O(n log n) here in general, because the input arrives unsorted. The best solution's win is memory: it reuses the array it was given instead of building a second list.
[[1, 10], [2, 3]] gives [[1, 10]].[[1, 4], [4, 5]] gives [[1, 5]].[[15, 18], [1, 3], [2, 6]] must still work."Sort by start, then sweep once" also inserts a new interval into a sorted list, counts how many meeting rooms are needed, and finds the busiest moment of a schedule.
Was this helpful?
O(n log n)The sort dominates; the merging pass after it is O(n).
O(n)The merged list is a second copy of the data.
O(n log n)Still the sort, then one O(n) pass.
O(1)Merged intervals are written back into the input; only a write index is extra.