How many rooms are needed at peak
A venue schedules sessions across the day. Find the minimum number of rooms so no two overlapping sessions share one.
- Each session has a start and an end in minutes.
- Intervals are half-open: [start, end).
- Sessions that merely touch (one ends exactly when the next starts) do not conflict.
session_rooms(starts: list<int>, ends: list<int>) → int
Where you start
def session_rooms(starts: list[int], ends: list[int]) -> int:
Worked examples
| Call | Result |
|---|---|
session_rooms([10], [20]) | 1 |
session_rooms([10, 10, 10], [20, 20, 20]) | 3 |
session_rooms([10, 15], [20, 25]) | 2 |
session_rooms([10, 20, 30], [15, 25, 35]) | 1 |
Hint
Sort starts and ends independently. Walk both lists with two pointers; the gap between active starts and ends at each step is the current room count — track the peak.
Reference solution in Python
def session_rooms(starts: list[int], ends: list[int]) -> int:
s = sorted(starts)
e = sorted(ends)
peak = 0
active = 0
j = 0
for i in range(len(s)):
active += 1
while j < len(e) and e[j] <= s[i]:
active -= 1
j += 1
if active > peak:
peak = active
return peak