Fewest Rooms to Host All Bookings
Greedy & IntervalsAuto-graded · 23 test casesLast updated
Find the smallest number of rooms that can host every booking at once, where a booking may begin the moment another one ends, using sorting and a min-heap.
Fewest Rooms to Host All Bookings
A venue receives a list of bookings. Each booking is a pair [start, end] of integers with start < end, meaning the room is occupied from time start until time end.
Return the smallest number of rooms that lets every booking be assigned to a room so that no room ever holds two bookings at the same moment.
Rules
- Two bookings in the same room conflict only if their time ranges overlap for a positive length. A booking that ends at time
tand another that starts at timetdo not conflict and may share a room. - Identical bookings are separate bookings. Two copies of
[3, 8]need two rooms. - The input list is in no particular order.
- If the list is empty, return
0.
Function
Implement rooms_needed(bookings), where bookings is a list of [start, end] pairs. It returns a single integer: the minimum number of rooms.
Constraints
0 <= len(bookings) <= 10^5- Each booking has exactly two integers,
start < end - Times are integers that fit in a normal 32-bit signed range
- The input list may be unsorted and may contain duplicates
- Do not rely on the input being modified. Work on a copy if you reorder it.
Examples
Example 1
Input: rooms_needed([[0, 30], [5, 10], [15, 20]])
Output: 2
Example 2
Input: rooms_needed([[7, 10], [10, 12]])
Output: 1
Example 3
Input: rooms_needed([[1, 5], [2, 6], [3, 7], [8, 9]])
Output: 3
Hints
Hint 1
Think about the busiest moment: the answer equals the largest number of bookings that are in progress at the same time.
Hint 2
Process bookings in order of start time. Keep track of the end times of the bookings currently occupying rooms, and be able to quickly find the one that finishes earliest.
Hint 3
A min-heap of end times works well. Before adding a new booking, remove every end time that is less than or equal to its start (touching is allowed). The largest heap size seen is the answer.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sort the bookings by start time and sweep through them in that order, while keeping a min-heap of the end times of the bookings that currently occupy a room.
For each booking [start, end]:
- Pop from the heap every end time that is
<= start. Those bookings are over (or just touching), so their rooms are free again. - Push the new booking's
endonto the heap. The heap size is now the number of rooms in use at timestart. - Track the largest heap size seen.
That maximum is the answer. An empty input never enters the loop and returns 0.
Why it is correct
- Lower bound: when a booking starts, every end time still in the heap belongs to a booking that began no later and ends strictly after this start. All of them, plus the new one, are running at that instant, so at least that many rooms are needed. The maximum over all starts is therefore a lower bound.
- Upper bound: the heap size at each step equals the number of rooms in use under the strategy "reuse any freed room, otherwise open a new one". Rooms are only added when everything is genuinely busy, so the number of rooms never exceeds the peak overlap.
- Since the lower and upper bounds coincide, the result is minimal.
- Using
<=when popping implements the rule that touching bookings do not conflict. Bookings with equal start times are handled correctly because they all pop the same set of finished bookings before being pushed.
Complexity
- Time:
O(n log n). Sorting costsO(n log n), and each booking is pushed and popped at most once, each inO(log n). - Space:
O(n)for the sorted copy and the heap.
Reference solution
import heapq
from typing import List
def rooms_needed(bookings: List[List[int]]) -> int:
ordered = sorted(bookings, key=lambda b: b[0])
busy_until = [] # min-heap of end times of bookings currently holding a room
most_rooms = 0
for start, end in ordered:
# Free every room whose booking has finished by this start time.
while busy_until and busy_until[0] <= start:
heapq.heappop(busy_until)
heapq.heappush(busy_until, end)
if len(busy_until) > most_rooms:
most_rooms = len(busy_until)
return most_rooms