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
- 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.
- Sort the start times and the end times separately.
- 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.
- 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…