Drill

ProblemsC# › events

How many rooms are needed at peak

hardeventsIntervalsSortingGreedyC#

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

C# 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

public int SessionRooms(List<int> starts, List<int> ends) {
    
}

Worked examples

CallResult
SessionRooms(new List<int> { 10 }, new List<int> { 20 })1
SessionRooms(new List<int> { 10, 10, 10 }, new List<int> { 20, 20, 20 })3
SessionRooms(new List<int> { 10, 15 }, new List<int> { 20, 25 })2
SessionRooms(new List<int> { 10, 20, 30 }, new List<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 C#
public int SessionRooms(List<int> starts, List<int> ends) {
    var s = new List<int>(starts);
    s.Sort();
    var e = new List<int>(ends);
    e.Sort();
    int peak = 0, active = 0, j = 0;
    for (int i = 0; i < s.Count; i++) {
        active++;
        while (j < e.Count && e[j] <= s[i]) { active--; j++; }
        if (active > peak) peak = active;
    }
    return peak;
}

The same problem in another language

More events problems in C#