Skip to content

Merge Intervals ​

↗ LeetCode: Merge Intervals · Medium · pattern chapter →

Asked atMetaAmazonGoogleMicrosoftBloombergAppleAdobe

Given a list of intervals, merge all overlapping ones and return the result.

Example 1 — [[1,3],[2,6],[8,10],[15,18]] → [[1,6],[8,10],[15,18]]Example 2 — [[1,4],[4,5]] → [[1,5]] (touching = merging by convention)

Constraints — 1 ≤ n ≤ 10⁴. Brute checks every pair for overlap in O(n²) = 10⁸ ops at n=10⁴. Sort by start (O(n log n)) then linear sweep with a single "current" interval is O(n log n) = ~10⁵ ops = <10 ms.

Progressive Hints
Try to solve it yourself first. Reveal hints only when stuck — each hint is progressively more specific.
---
⏱️Interview timer00:00
Practice under interview pressure. Start when you begin reading; stop when you finish. Save best only when you had a full working solution.
Target for interview: Medium = 20-25 min · Hard = 30-40 min · Best time persists on this device only.

Approach 1 — Brute force (compare every pair) ​

Intuition. For each pair, if they overlap, merge and restart.

java
int[][] mergeBrute(int[][] intervals) {
    List<int[]> list = new ArrayList<>();
    for (int[] i : intervals) list.add(i);
    boolean changed = true;
    while (changed) {
        changed = false;
        for (int i = 0; i < list.size(); i++)
            for (int j = i + 1; j < list.size(); j++) {
                int[] a = list.get(i), b = list.get(j);
                if (a[1] >= b[0] && b[1] >= a[0]) {
                    list.set(i, new int[]{Math.min(a[0], b[0]), Math.max(a[1], b[1])});
                    list.remove(j);
                    changed = true;
                    break;
                }
            }
    }
    return list.toArray(new int[0][]);
}

Complexity — Time O(n³); Space O(n). Ugly.


Approach 2 — Sort + one-pass sweep ​

Insight from brute. Sort by start. Then overlap is a local decision: does the current interval start ≤ last-merged end? If yes, extend the end. If no, start a new bucket.

java
int[][] merge(int[][] intervals) {
    Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
    List<int[]> out = new ArrayList<>();
    int[] last = intervals[0];
    out.add(last);
    for (int i = 1; i < intervals.length; i++) {
        int[] cur = intervals[i];
        if (cur[0] <= last[1]) last[1] = Math.max(last[1], cur[1]);
        else { last = cur; out.add(last); }
    }
    return out.toArray(new int[0][]);
}
Execution Trace
Sort + sweep — [[1,3],[2,6],[8,10],[15,18]]
i[1,3]0[2,6]1[8,10]2[15,18]3last = [1,3]out = [[1,3]]
Step 1 / 4seed with first

Complexity — Time O(n log n) (sorting dominates); Space O(n) for output. Optimal.


Try it yourself ​

Complexity summary ​

ApproachTimeSpace
Pairwise merge until stableO(n³)O(n)
Sort + sweepO(n log n)O(n)

When to use which ​

  • Cold interview → jump straight to sort+sweep (brute is only worth stating).
  • Touching vs strict overlap — clarify with the interviewer. cur[0] <= last[1] merges touching; cur[0] < last[1] keeps them separate.
✨AI Study CompanionBeta
Was this page helpful?

Released under MIT license.

💡 Quick shortcuts
H HintsR Run JavaA AI Help