Drill

ProblemsJava › events

How many rooms are needed at peak

hardeventsIntervalsSortingGreedyJava

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

Java 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

int sessionRooms(List<Integer> starts, List<Integer> ends) {
    
}

Worked examples

CallResult
sessionRooms(Main.<Integer>ls(10), Main.<Integer>ls(20))1
sessionRooms(Main.<Integer>ls(10, 10, 10), Main.<Integer>ls(20, 20, 20))3
sessionRooms(Main.<Integer>ls(10, 15), Main.<Integer>ls(20, 25))2
sessionRooms(Main.<Integer>ls(10, 20, 30), Main.<Integer>ls(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 Java
int sessionRooms(List<Integer> starts, List<Integer> ends) {
    List<Integer> s = new ArrayList<>(starts);
    Collections.sort(s);
    List<Integer> e = new ArrayList<>(ends);
    Collections.sort(e);
    int peak = 0, active = 0, j = 0;
    for (int i = 0; i < s.size(); i++) {
        active++;
        while (j < e.size() && e.get(j) <= s.get(i)) { active--; j++; }
        if (active > peak) peak = active;
    }
    return peak;
}

The same problem in another language

More events problems in Java