Count Rooms In Use at Peak Time
Greedy & IntervalsAuto-graded · 15 test casesLast updated
Tests the sweep-line technique by finding the maximum number of meeting rooms simultaneously occupied across a set of half-open booking intervals.
Count Rooms In Use at Peak Time
A building has an unlimited number of meeting rooms. You are given a list of bookings, where each booking is represented as [start, end] — a half-open interval [start, end) measured in minutes from midnight. A room is considered occupied for the entire duration start ≤ t < end.
Your task is to find the maximum number of rooms that are in use at the same time across all bookings.
Two bookings [s1, e1) and [s2, e2) overlap if and only if they share at least one common minute, i.e. max(s1, s2) < min(e1, e2).
Return 0 if no bookings are provided.
Constraints
0 ≤ len(bookings) ≤ 2000- Each booking is a list of exactly two integers
[start, end]. 0 ≤ start < end ≤ 1440(minutes in a day)- Multiple bookings may have identical
[start, end]values.
Examples
Example 1
Input: peak_rooms_in_use([[0, 60], [30, 90], [60, 120]])
Output: 2
Example 2
Input: peak_rooms_in_use([[0, 30], [30, 60], [60, 90]])
Output: 1
Example 3
Input: peak_rooms_in_use([])
Output: 0
Hints
Hint 1
Think of each booking as two events: a +1 at the start minute and a -1 at the end minute. Process these events in sorted order.
Hint 2
When two events fall on the same minute, make sure you process all end events (-1) before start events (+1) — this correctly handles the half-open interval semantics.
Hint 3
Keep a running counter of currently active rooms as you sweep through the sorted events, and track its maximum value.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach: Sweep Line with Event Points
Intuition
Rather than checking every possible minute from 0 to 1440, we only need to check the moments when the number of active rooms actually changes — at the start and end of each booking.
For each booking [s, e):
- At minute
s, one more room becomes occupied (+1event). - At minute
e, that room is freed (-1event).
We collect all such events, sort them by time, and sweep through them while maintaining a running counter. The maximum value the counter ever reaches is our answer.
Handling ties correctly
Because intervals are half-open ([start, end)), a booking ending at minute t does not conflict with one starting at the same minute t. To enforce this, when two events share the same timestamp we must process end events (-1) before start events (+1). Sorting by the tuple (time, delta) achieves this automatically: -1 < +1, so endings are processed first.
Why it is correct
After sorting with the tie-break rule, the running counter at any point equals exactly the number of rooms currently occupied. We never double-count an ending room and a starting room at the same minute. The peak of this counter is therefore the correct answer.
Complexity
- Time: O(n log n) — dominated by sorting 2n events.
- Space: O(n) — for the events list.
Reference solution
def peak_rooms_in_use(bookings: list) -> int:
if not bookings:
return 0
events = []
for start, end in bookings:
events.append((start, 1)) # room becomes occupied
events.append((end, -1)) # room becomes free (half-open: end is excluded)
# Sort by time; for ties, process -1 (end) before +1 (start)
# so that [s, e) and [e, f) are NOT counted as overlapping.
events.sort(key=lambda x: (x[0], x[1]))
peak = 0
current = 0
for _time, delta in events:
current += delta
if current > peak:
peak = current
return peak