Maximum Simultaneous Room Bookings
Greedy & IntervalsAuto-graded · 15 test casesLast updated
Tests the sweep line technique on interval scheduling to find the peak number of overlapping room bookings over a timeline.
Maximum Simultaneous Room Bookings
A conference centre logs every room booking as a half-open interval [start, end) — the room is occupied from minute start up to but not including minute end.
Given a list of such bookings, find the maximum number of bookings that are simultaneously active at any single point in time, and return the earliest minute at which that maximum is first reached.
Formally, for a query minute t, a booking [s, e) is active if s <= t < e. You must return a tuple [peak_count, earliest_minute].
If the booking list is empty, return [0, -1].
Constraints
0 <= len(bookings) <= 2000- Each booking is a list
[start, end]with0 <= start < end <= 10^6 - All
startandendvalues are integers - Multiple bookings may share the same
startorend - There is always a unique answer for
earliest_minutebecause ties are broken by taking the smallest such minute
Examples
Example 1
Input: max_simultaneous_bookings([[1, 5], [2, 6], [4, 8], [7, 10]])
Output: [3, 4]
Example 2
Input: max_simultaneous_bookings([[0, 3], [3, 6], [6, 9]])
Output: [2, 3]
Example 3
Input: max_simultaneous_bookings([])
Output: [0, -1]
Hints
Hint 1
Create two types of events: +1 at each booking's start and -1 at each booking's end, then process them in time order — this is the classic sweep line setup.
Hint 2
When multiple events fall at the same minute, process all +1 (start) events before all -1 (end) events, so a room that starts exactly when another ends counts as overlapping.
Hint 3
Track a running count as you sweep; whenever it exceeds the current maximum, update both the peak count and the earliest minute where it was reached.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight: Sweep Line with Typed Events
Because bookings are half-open intervals [s, e), a booking starts being active at minute s and stops being active at minute e. The sweep line technique converts this into a sequence of point events:
- Start event at
s: active count increases by 1. - End event at
e: active count decreases by 1.
We want the maximum value of the running count at any integer minute, and specifically the first minute it reaches that maximum.
Sort Order — Why Starts Before Ends at the Same Time
If a booking ends at minute t and another begins at minute t, the half-open semantics means they do overlap at minute t (the new one is active, the old one is not — they do NOT overlap). Wait, let's be precise: [s1, e) is active for t where s1 <= t < e, so at t = e it is not active. A booking [e, e2) is active at t = e. They share no minute.
However, if two bookings [s, e) and [s, e2) both start at the same minute s, the count must reflect both active at minute s. So when events share the same time, starts (type 0) must be processed before ends (type 1). This is achieved by using (time, 0) for starts and (time, 1) for ends and sorting lexicographically.
With this ordering, at any given minute we first apply all arrivals, then check the count (implicitly — we record the peak whenever current exceeds it after an arrival), then apply departures.
Correctness
- The running counter after processing all start-events at minute
t(before any end-events) equals the number of bookings active at that minutet, because a booking[s, e)is active iffs <= t < e. - We record
earliest_minuteonly when the count strictly exceeds the previous peak, guaranteeing we capture the first minute the global maximum is reached.
Complexity
| Value | |
|---|---|
| Time | O(n log n) — dominated by sorting 2n events |
| Space | O(n) — for the events list |
where n = number of bookings.
Edge Cases Covered
- Empty list →
[0, -1] - Single booking → peak is 1 at its start minute
- All bookings at the same start → peak equals total count, earliest minute is that shared start
- No overlaps → peak is 1, earliest minute is the first booking's start
- Back-to-back bookings sharing a boundary minute (e.g.,
[0,5)and[5,10)) → they do not overlap; peak stays 1
from typing import List
def max_simultaneous_bookings(bookings: List[List[int]]) -> List[int]:
if not bookings:
return [0, -1]
# Build sweep line events: (time, type) where type 0 = start (+1), type 1 = end (-1)
# Sorting by (time, type) ensures starts are processed before ends at the same minute.
events = []
for s, e in bookings:
events.append((s, 0)) # 0 < 1 so starts come first
events.append((e, 1))
events.sort()
peak_count = 0
earliest_minute = -1
current = 0
for time, etype in events:
if etype == 0:
current += 1
if current > peak_count:
peak_count = current
earliest_minute = time
else:
current -= 1
return [peak_count, earliest_minute]