Skip to content
StackDecode
GreedyAnimated

Interval Scheduling

Pick the interval that finishes earliest.

Time

O(n log n)

Extra space

O(1)

Use it for

Maximum non-overlapping set.

How it works

  1. This is the same problem as activity selection: pick the most non-overlapping intervals.
  2. Sort the activities by finishing time.
  3. Take the first one. It finishes earliest, so it blocks the least.
  4. Walk down the list: take an activity only if it starts at or after the end of the last one taken.
  5. Greedy is optimal here: an exchange argument shows some best schedule always starts with the earliest finisher. O(n log n).

Watch it run

Loading animation…