Skip to content
Problem 56 · LeetCode 56(opens in a new tab)

Merge Intervals

MediumArrays

You get a list intervals, where each item is a pair [start, end] with start ≤ end. Two intervals overlap when they share at least one point, and that includes intervals that only touch, like [2, 5] and [5, 9].

Combine every group of overlapping intervals into one interval that covers the whole group. Return the intervals that are left once nothing overlaps. The input can be in any order, and so can your answer.

Example 1

Input:  intervals = [[2, 5], [12, 14], [4, 8], [10, 11]]
Output: [[2, 8], [10, 11], [12, 14]]
Why:    [2, 5] and [4, 8] share 4 to 5; the rest stand alone
2 more examples and the constraints

Example 2

Input:  intervals = [[3, 6], [6, 9]]
Output: [[3, 9]]
Why:    they touch at 6, which counts as overlapping

Example 3

Input:  intervals = [[1, 10], [2, 3], [4, 5]]
Output: [[1, 10]]
Why:    the two short intervals sit inside [1, 10]

Constraints

  • 1 ≤ number of intervals ≤ 10,000
  • Each interval is a pair [start, end]
  • 0 ≤ start ≤ end ≤ 10,000

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Overlap graphO(n²)
  2. Sort, then sweepO(n log n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 10,000 intervalsVerdict
Overlap graphO(n²)
Mention it, then move on
Sort, then sweepO(n log n)
Best; one sort, one pass

n = how many intervals are in the list.

Edge cases to test

  • Touching ends. [3, 6] and [6, 9] share the point 6, so they merge into [3, 9]. Use ≤, not <.
  • Nested intervals. In [[1, 10], [2, 3]], the second ends before the first. The merged end must stay 10, so take the max.
  • Unsorted input. [[9, 12], [6, 9], [3, 6], [0, 3]] is one chain in reverse. Without sorting first, the sweep misses it.
  • Neighbours that do not touch. [0, 0] and [1, 1] share no point, so they stay apart.
  • One interval. [[4, 7]] comes back unchanged.

Common mistakes

  • Forgetting to sort first, so overlapping intervals far apart in the list never meet.
  • Setting the merged end to the new end instead of the larger of the two, which shrinks [1, 10] when [2, 3] arrives.
  • Checking start < last end instead of ≤, which leaves touching intervals like [3, 6] and [6, 9] apart.

How to talk through it

  1. 1Clarify. Do touching intervals count as overlapping? Is the input sorted? Can I sort it in place, and does the output order matter?
  2. 2Give the graph idea. Connect every overlapping pair and merge each connected group. Say it is O(n²) because it compares every pair.
  3. 3Name what sorting buys. After sorting by start, an interval can only overlap the last merged one, so one pass is enough.
  4. 4Code the sweep. Compare the start with the last merged end using ≤, and extend with max so nested intervals do not shrink it.
  5. 5Test out loud. Run touching ends, a nested interval and an unsorted list before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?