Skip to content
StackDecode
GreedyAnimated

Interval Partitioning

Assign each interval to a free resource, opening a new one only if needed.

Time

O(n log n)

Extra space

O(n)

Use it for

Fewest rooms or machines.

How it works

  1. Interval partitioning asks for the fewest resources (rooms) so that no two overlapping intervals share one. The answer equals the maximum number of intervals overlapping at any moment.
  2. Sort the start times and the end times separately.
  3. Walk through the starts. If a meeting starts before the earliest unfinished meeting ends, it needs a new room. Otherwise it reuses a room that has just been freed.
  4. A meeting ending exactly when another starts shares a room. The number of rooms ever needed at once is the answer. O(n log n).

Watch it run

Loading animation…