Fewest Conference Rooms for a Day of Bookings
Greedy & IntervalsAuto-graded · 20 test casesLast updated
Compute the minimum number of rooms needed to host all time-range bookings when back-to-back bookings may reuse a room, testing sweep-line or heap-based interval reasoning.
Fewest Conference Rooms for a Day of Bookings
You manage a building's conference rooms and receive a list of bookings. Each booking is a pair [start, end] of integers with start < end, giving the time the booking begins and the time it finishes.
Treat each booking as occupying the half-open time span from start up to, but not including, end. So a booking that finishes at time 10 and another that begins at time 10 do not clash, and the same room can serve both one after the other.
Write a function that returns the smallest number of rooms needed so that every booking is assigned to a room and no room ever hosts two bookings whose time spans overlap.
Rules
- The bookings may be given in any order, and several bookings may be identical.
- If the list is empty, the answer is
0. - Return a single integer.
Constraints
0 <= len(bookings) <= 10^5- Each booking is
[start, end]with integers andstart < end - Bookings are not guaranteed to be sorted
- Bookings that share only an endpoint (one ends exactly when another starts) do not overlap
Examples
Example 1
Input: min_rooms_required([[9, 10], [9, 12], [11, 13], [14, 15]])
Output: 2
Example 2
Input: min_rooms_required([[0, 30], [5, 10], [15, 20]])
Output: 2
Hints
Hint 1
Think about the moment in time when the most bookings are running at once. That peak count is the number of rooms you need.
Hint 2
Sort the bookings by start time. As you go, you need to know whether any room that is already in use has become free by the current start time.
Hint 3
Keep a min-heap of end times of rooms in use. Before adding a booking, pop every end time that is less than or equal to its start (equal means the room is free). The largest heap size seen is the answer.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The answer equals the largest number of bookings running at the same moment. At least that many rooms are required, because those simultaneous bookings must all be in different rooms. That many is also enough, as the greedy procedure below never needs more.
Process the bookings in order of start time while keeping a min-heap of the end times of the rooms currently in use:
- Sort the bookings by start time.
- For each booking, pop from the heap every end time that is
<= start. Those rooms are free, and using<=implements the rule that a booking ending attdoes not clash with one starting att. - Push the current booking's end time. The heap now holds one entry per room that is busy at this start time.
- Track the maximum heap size seen. That is the answer.
Why it is correct
When a booking starts at time s, the heap contains exactly the bookings that began no later than s and have not yet ended, so its size is the number of simultaneous bookings at s. The peak overlap always occurs at some booking's start time, so the maximum heap size is the peak overlap. Reusing a freed room (by popping its entry) never harms anything, since all remaining bookings start no earlier than the current one.
An empty list returns 0.
Complexity
- Time:
O(n log n)for sorting plusO(n log n)for heap operations. - Space:
O(n)for the heap in the worst case. Note thatsortedcreates a copy, so the caller's list is not modified.
Reference solution
import heapq
from typing import List
def min_rooms_required(bookings: List[List[int]]) -> int:
if not bookings:
return 0
end_times = []
most = 0
for start, end in sorted(bookings):
while end_times and end_times[0] <= start:
heapq.heappop(end_times)
heapq.heappush(end_times, end)
most = max(most, len(end_times))
return most