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.
sessionRooms(starts: list<int>, ends: list<int>) → int
Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
func sessionRooms(starts []int, ends []int) int {
}
Worked examples
| Call | Result |
|---|---|
sessionRooms([]int{10}, []int{20}) | 1 |
sessionRooms([]int{10, 10, 10}, []int{20, 20, 20}) | 3 |
sessionRooms([]int{10, 15}, []int{20, 25}) | 2 |
sessionRooms([]int{10, 20, 30}, []int{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 Go
func sessionRooms(starts []int, ends []int) int {
s := append([]int{}, starts...)
sort.Ints(s)
e := append([]int{}, ends...)
sort.Ints(e)
peak, active, j := 0, 0, 0
for i := 0; i < len(s); i++ {
active++
for j < len(e) && e[j] <= s[i] {
active--
j++
}
if active > peak {
peak = active
}
}
return peak
}