Smallest-ID Room Allocation for Booking Time Slots
Greedy & IntervalsAuto-graded · 22 test casesLast updated
Partition half-open time intervals across the fewest resources, deterministically giving each booking the lowest-numbered free resource, using sorting and two heaps.
Allocate Rooms to Bookings
A venue receives a list of bookings. Booking i is given as bookings[i] = [start_i, end_i] and uses a room during the half-open time span [start_i, end_i). A room that is released at time end_i can be given to another booking that starts at any time >= end_i.
Assign every booking to a room so that:
- Two bookings placed in the same room never overlap in time.
- The number of rooms used is as small as possible.
- The allocation is deterministic. Bookings are handled in ascending order of
start; bookings with the samestartare handled in ascending order of their original index. - When a booking is handled, if one or more rooms are free, it takes the free room with the smallest room number. If no room is free, it takes the smallest room number that has never been used. Rooms are numbered
1, 2, 3, ....
A room counts as free for a booking starting at time t if every booking previously placed in it has an end that is <= t.
Return a tuple (room_count, assignment), where room_count is the minimum number of rooms needed and assignment[i] is the room number given to booking i (in the original input order).
If bookings is empty, return (0, []).
Constraints
0 <= len(bookings) <= 10^50 <= start_i < end_i <= 10^9- All values are integers.
- Rooms released at time
tmust be treated as free for any booking that starts at timet.
Examples
Example 1
Input: allocate_rooms([[1, 4], [2, 5], [4, 7], [3, 6]])
Output: [3, [1, 2, 1, 3]]
Example 2
Input: allocate_rooms([[1, 3], [3, 5]])
Output: [1, [1, 1]]
Hints
Hint 1
Process bookings in sorted order of (start, original index), but write each result back to the booking's original position.
Hint 2
Keep a min-heap of occupied rooms keyed by end time, so you can find every room that has been released by the current start time.
Hint 3
A heap keyed by end time alone cannot tell you the smallest free room number. Move all released rooms (end <= start) into a second min-heap of free room numbers, and pop the smallest from it.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is interval partitioning with a fixed tie-breaking rule.
- Sort booking indices by
(start, index). This is exactly the required processing order. - Maintain two min-heaps:
occupied: pairs(end, room)for rooms currently in use.free: room numbers that are currently free.
- For each booking in order, first move every room from
occupiedwithend <= startintofree. All such rooms must be moved, not just one, because the smallest room number among all released rooms must be chosen. - If
freeis non-empty, pop the smallest room number. Otherwise open a new room with the next unused number. - Record the room at the booking's original index and push
(end, room)ontooccupied. - The room count is the number of room numbers ever opened.
Why it is correct
- Rules 3 and 4 are enforced directly: the order is
(start, index), released rooms are all freed before assigning (so touching intervals reuse a room), the smallest free number is chosen, and new numbers are issued in increasing order. - Minimality: suppose room
kis opened at timep. Then no room was free, so rooms1..k-1each hold a booking that started at or beforepand ends afterp. Together with the current booking,kbookings contain timep. Overlapping bookings need distinct rooms, so at leastkrooms are required, and the greedy uses exactlyk. - Draining all released rooms matters. Releasing only the earliest-ending room could hand out a larger room number even though a smaller one is also free.
Complexity
- Time: O(n log n) for the sort and the heap operations (each booking is pushed to and popped from each heap at most once).
- Space: O(n) for the order, heaps and output.
Reference solution
import heapq
from typing import List, Tuple
def allocate_rooms(bookings: List[List[int]]) -> Tuple[int, List[int]]:
n = len(bookings)
if n == 0:
return (0, [])
order = sorted(range(n), key=lambda i: (bookings[i][0], i))
occupied = [] # min-heap of (end time, room number)
free = [] # min-heap of free room numbers
assignment = [0] * n
next_room = 1
for i in order:
start, end = bookings[i]
while occupied and occupied[0][0] <= start:
_, room = heapq.heappop(occupied)
heapq.heappush(free, room)
if free:
room = heapq.heappop(free)
else:
room = next_room
next_room += 1
assignment[i] = room
heapq.heappush(occupied, (end, room))
return (next_room - 1, assignment)