Fewest Rooms Needed for a Day of Sessions
Solve this ProblemA conference has a list of sessions, each with a start and an end time. A session occupies its room from its start up to (not including) its end, so a room can host a session that begins at the moment the previous one finishes. Find the smallest number of rooms that lets every session run at its scheduled time.
Scanning a list of rooms for a free one works, but a min-heap of end times always hands over the room that frees up first in O(log n).
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
1 ≤ sessions.length ≤ 12; each session is [start, end] with 0 ≤ start < end ≤ 40 - ◆
A session occupies its room from start up to (not including) end, so a room can host a session that starts exactly when the previous one ends - ◆
Sessions may be assigned to any room. Return the fewest rooms that let every session run at its scheduled time
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
Approach & Solutions
Brute Force — Assign Sessions to Rooms by Scanning the Room List
BruteProcess the sessions in order of start time while keeping a list with the time each existing room becomes free. For each session, scan the list from the front for any room that is already free by the session's start; if found, put the session there (that room is now free at the session's end), otherwise open a new room. The answer is the number of rooms opened. It is correct, but each session may scan every room, so the cost grows with sessions × rooms.
O(n log n + n · rooms)O(rooms)1class Solution {
2 public int fewestRooms(int[][] sessions) {
3 int[][] byStart = sessions.clone();
4 Arrays.sort(byStart, (a, b) -> a[0] - b[0]);
5 List<Integer> roomFreeAt = new ArrayList<>();
6 for (int[] session : byStart) {
7 boolean placed = false;
8 for (int r = 0; r < roomFreeAt.size(); r++) {
9 if (roomFreeAt.get(r) <= session[0]) {
10 roomFreeAt.set(r, session[1]);
11 placed = true;
12 break;
13 }
14 }
15 if (!placed) roomFreeAt.add(session[1]);
16 }
17 return roomFreeAt.size();
18 }
19}Optimal — Min-Heap of Room End Times
OptimalSort the sessions by start time and keep a min-heap of the end times of the sessions currently holding a room, so the root is the room that frees up first. For each session: if the root's end time is at or before the session's start, that room has just become free — pop it (the session takes over that room); either way push the session's end time. The heap's size is the number of rooms in use, and since it only grows when no room is free, its final size is the fewest rooms needed. Finding the room that frees up first costs O(log n) instead of a scan.
O(n log n)O(n)1class Solution {
2 public int fewestRooms(int[][] sessions) {
3 int[][] byStart = sessions.clone();
4 Arrays.sort(byStart, (a, b) -> a[0] - b[0]);
5 PriorityQueue<Integer> ends = new PriorityQueue<>();
6 for (int[] session : byStart) {
7 if (!ends.isEmpty() && ends.peek() <= session[0]) ends.poll();
8 ends.offer(session[1]);
9 }
10 return ends.size();
11 }
12}