Skip to content
StackDecode
GreedyAnimated

Non-overlapping Intervals

Remove the fewest intervals by keeping those that end earliest.

Time

O(n log n)

Extra space

O(1)

Use it for

Interval cleanup.

How it works

  1. To remove as few intervals as possible, keep as many non-overlapping ones as possible.
  2. That is exactly activity selection: sort by end, keep each interval that starts at or after the previous kept end.
  3. Everything not kept must be removed. Intervals that only touch (end = next start) do not overlap.

Watch it run

Loading animation…