Merge Intervals

Combine time ranges that overlap into the fewest ranges possible.

The idea (calendar meetings)

You have meetings at 1–3, 2–6 and 8–10. The first two overlap, so your calendar really shows one busy block from 1 to 6, plus 8–10. Merging intervals does that for any list.

The trick is to sort by start time first. Then overlapping ranges become neighbors, and one left-to-right sweep is enough:

Watch it work

Try it

Common mistakes

Complexity, in plain English: sorting costs the most, about n log n steps (O(n log n)). The sweep afterwards is a single pass (O(n)).