Fewest Conference Rooms for a Day of Bookings
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Find the fewest rooms that can host every booking in an unordered list of half-open time ranges, where back-to-back bookings may share a room.
Fewest Conference Rooms for a Day of Bookings
A office has a list of bookings, and each booking has a start time and an end time. A room can hold only one booking at any moment, and a booking must stay in a single room from its start until its end.
Write a function that returns the smallest number of rooms that lets every booking take place in full.
Input
bookings: a list of pairs[start, end]. Pairidescribes bookingi, which occupies the half-open time range[start, end).
Output
- An integer: the minimum number of rooms required.
Rules
- Two bookings clash if their time ranges overlap.
- Because ranges are half-open, a booking that ends at time
tdoes not clash with a booking that starts at timet. Both can use the same room. - The bookings come in no particular order.
- If the list is empty, return
0.
Constraints
0 <= len(bookings) <= 10^50 <= start < end <= 10^9for every booking
Examples
Example 1
Input: min_rooms_required([[1, 5], [2, 6], [4, 8], [7, 9]])
Output: 3
Example 2
Input: min_rooms_required([[1, 3], [3, 5], [5, 7]])
Output: 1
Example 3
Input: min_rooms_required([[10, 20], [0, 10], [5, 15]])
Output: 2
Hints
Hint 1
Think about a single moment in time: how many bookings are running at once? What does that say about the number of rooms needed?
Hint 2
Turn each booking into two events, one where a room becomes occupied and one where a room becomes free, then process the events in time order while keeping a running count.
Hint 3
When an end event and a start event share the same time, handle the end first so that back-to-back bookings do not count as overlapping.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The minimum number of rooms equals the largest number of bookings that are in progress at the same instant. We find that value with a sweep over time.
- For every booking
[s, e), create two events:(s, +1)for a start and(e, -1)for an end. - Sort all events by time. For equal times, the tuple ordering places
-1before+1, so ends are processed before starts. - Walk through the events, adding each delta to a running counter. The highest value the counter reaches is the answer.
Why it works
- Lower bound: if
kbookings are in progress at the same instant, each needs its own room, so at leastkrooms are required. - Upper bound: assign bookings in order of start time, reusing any free room and opening a new one only if none is free. If a new room number
kis opened at times, then the otherk - 1rooms all hold bookings that are still running ats, so together with the new one there areksimultaneous bookings. Hence the greedy never needs more rooms than the peak overlap. - Boundary rule: processing ends before starts at the same time means a booking ending at
tis removed from the count before one starting attis added, which matches the half-open definition. With the opposite order,[[1,3],[3,5]]would wrongly report 2.
Complexity
- Time: O(n log n) for sorting the 2n events.
- Space: O(n) for the event list.
Times up to 10^9 are only compared, never used as indices, so their magnitude does not matter.
Reference solution
from typing import List
def min_rooms_required(bookings: List[List[int]]) -> int:
events = []
for s, e in bookings:
events.append((s, 1))
events.append((e, -1))
# At equal times, -1 sorts before +1, so ends are handled before starts.
events.sort()
current = 0
best = 0
for _, delta in events:
current += delta
if current > best:
best = current
return best