GreedyAnimated
Meeting Rooms
Sort and sweep to find how many rooms are needed.
Time
O(n log n)
Extra space
O(n)
Use it for
Calendar conflicts.
How it works
- 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…