Drill

ProblemsGo › events

How many rooms are needed at peak

hardeventsIntervalsSortingGreedyGo

A venue schedules sessions across the day. Find the minimum number of rooms so no two overlapping sessions share one.

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.

Solve it in Python →

Where you start

func sessionRooms(starts []int, ends []int) int {
	
}

Worked examples

CallResult
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
}

The same problem in another language

More events problems in Go