Lowest-Numbered Free Room Booking for Time Slots
Heaps, Sorting & SearchAuto-graded · 20 test casesLast updated
Simulate a deterministic room-allocation process with heaps: sort by start, free finished rooms, pick the smallest free ID, and report assignments in input order.
Booking Rooms by Lowest Free Number
A building receives a list of bookings, where slots[i] = [begin_i, finish_i] describes a booking that occupies a room during the half-open time range [begin_i, finish_i). A booking that finishes at time t releases its room at that same moment, so another booking that begins at time t may use that room.
Each booking must be given a room number, and two bookings whose time ranges overlap must never share a room. The assignment follows a fixed procedure:
- Handle bookings in increasing order of
begin. If several bookings have the samebegin, handle the one with the smaller original index first. - Just before handling a booking that begins at time
b, every room whose current booking hasfinish <= bbecomes free. - Give the booking the free room with the smallest number (rooms are numbered from
0). If no room is free, open a new room whose number equals the count of rooms opened so far (so0, then1, then2, and so on).
Return a list answer of length len(slots) where answer[i] is the room number given to slots[i], in the original input order.
Constraints
1 <= len(slots) <= 10^5len(slots[i]) == 20 <= begin_i < finish_i <= 10^9
Examples
Example 1
Input: book_rooms([[0, 30], [5, 10], [15, 20]])
Output: [0, 1, 1]
Example 2
Input: book_rooms([[1, 4], [2, 5], [4, 6]])
Output: [0, 1, 0]
Example 3
Input: book_rooms([[1, 2], [2, 3], [3, 4]])
Output: [0, 0, 0]
Hints
Hint 1
Process bookings in sorted order of (begin, original index), but remember to write each result back at its original index.
Hint 2
Keep two min-heaps: one of busy rooms keyed by finish time, and one of free room numbers. Before placing a booking, move every room with finish <= begin into the free heap.
Hint 3
Use <= (not <) when releasing rooms so back-to-back bookings reuse the same room. If the free heap is empty, open a new room numbered by how many have been opened so far.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The rules are fully deterministic, so the task is a simulation made fast with two min-heaps.
- Sort the booking indices by
(begin, index). This gives the required processing order and lets us write each answer back to its original position. - Keep a min-heap
busyof(finish, room)pairs and a min-heapfreeof room numbers. - For each booking in order, pop every entry from
busywithfinish <= beginand push its room intofree. The<=implements the half-open interval rule. - If
freeis non-empty, take its smallest room number. Otherwise open a new room numbered by the count of rooms opened so far. - Record the room in the answer and push
(finish, room)ontobusy.
Why it is correct
- Processing in
(begin, index)order matches rule 1, including the tie-break. - Because begin times never decrease, a room released at some point stays free for all later bookings, so releasing lazily when each booking arrives matches rule 2.
- The
freeheap holds exactly the currently free rooms, ordered by number, so its minimum is the smallest free room (rule 3). New numbers are issued sequentially only when no room is free.
Complexity
- Time: O(n log n) for sorting plus heap operations; each booking is pushed to and popped from each heap at most once.
- Space: O(n) for the order, the answer, and the heaps.
Reference solution
import heapq
from typing import List
def book_rooms(slots: List[List[int]]) -> List[int]:
order = sorted(range(len(slots)), key=lambda i: (slots[i][0], i))
answer = [-1] * len(slots)
busy = []
free = []
opened = 0
for idx in order:
begin, finish = slots[idx]
while busy and busy[0][0] <= begin:
_, room = heapq.heappop(busy)
heapq.heappush(free, room)
if free:
room = heapq.heappop(free)
else:
room = opened
opened += 1
answer[idx] = room
heapq.heappush(busy, (finish, room))
return answer