Drill

ProblemsPython › events

How many rooms are needed at peak

hardeventsPython

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

session_rooms(starts: list<int>, ends: list<int>) → int

Solve it in the editor →

Where you start

def session_rooms(starts: list[int], ends: list[int]) -> int:
    

Worked examples

CallResult
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

The same problem in another language

More events problems in Python