Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Concept

Overview

Sort, then sweep · overlap ⇔ b.start ≤ a.end
step 1 / 5
[1,3]
[2,5]
[7,8]
0
1
2
3
4
5
6
7
8
[0][1][2][3][4][5][6][7][8]
The intervals skeleton
1sort intervals by start
2// overlap(A, B) := B.start <= A.end
3cur ← intervals[0]
4for B in intervals[1:]:
5 if B.start <= cur.end: cur.end = max(cur.end, B.end)
6 else: emit cur; cur ← B
7emit cur
state
  • intervals[1,3] [2,5] [7,8]

line 1An INTERVAL is a range [start, end] on a line. Interval problems — merging, scheduling, counting overlaps — almost all yield to one move: SORT the intervals first (usually by start), then sweep through them left → right in a single pass.