intermediate · intervals · sorting · sweep line · two pointers
How many rooms for these meetings?
Problem
Meetings are given as half-open time ranges [start, end): a meeting ending at 10 frees its room for a meeting starting at 10. What is the smallest number of rooms that lets every meeting take place, with no two meetings in the same room at the same time?
In the lab, enter the meetings as one list: start end start end …
Constraints
- 0 ≤ meetings ≤ 16
- 0 ≤ start < end ≤ 99
Examples
[[0, 30], [5, 10], [15, 20]] → 2
[[1, 4], [4, 6]] → 1 — The second starts exactly when the first ends.
Show the solution
Sorted starts and ends, two pointers
Walking through time, each start needs a room and each end returns one. Pairing starts with ends separately works because rooms are interchangeable.
Why it is correct
Every start either reuses a room (j grows) or opens one (rooms grows), so rooms + j = i before the i-th start s in sorted order; the j smallest ends have handed their rooms back. If s ≥ ends[j], the meeting with the earliest unreturned end is over by s, so its room is reused. If s < ends[j], every unreturned end is after s, so the i − j = rooms meetings holding rooms are all still running at s, and together with the new one rooms + 1 meetings overlap: a new room is unavoidable. So rooms never exceeds the largest number of meetings overlapping at one moment, which every valid assignment needs, and the sweep itself is an assignment that uses rooms rooms.
Complexity
Time O(n log n), extra space O(n). Two sorts of n numbers, then one pass over the starts with a pointer that only moves forward.
For contrast: For each meeting's start time t, count meetings with start ≤ t < end; take the maximum. That costs O(n²) time and O(1) space.
Edge cases
- No meetings: 0.
- Back-to-back meetings share a room (half-open ranges).
- Identical meetings each need a room.
Common mistakes
- Treating ranges as closed, so back-to-back meetings need two rooms.
- Merging the intervals first, which loses the overlap count.
Step through it
The solution in four languages
Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.
namespace Bekadan.Dsa.Examples.Problems;
public static class RoomsNeeded
{
/// <summary>Fewest rooms for half-open meetings [start, end). Sorted starts and ends with two pointers: O(n log n).</summary>
public static int Rooms(IReadOnlyCollection<(int Start, int End)> meetings)
{
int[] starts = meetings.Select(m => m.Start).Order().ToArray();
int[] ends = meetings.Select(m => m.End).Order().ToArray();
int rooms = 0, j = 0;
foreach (int s in starts)
{
if (s >= ends[j])
{
j++; // the earliest-ending meeting is over: take its room
}
else
{
rooms++;
}
}
return rooms;
}
}
Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/RoomsNeeded.cs
from collections.abc import Sequence
def rooms(meetings: Sequence[tuple[int, int]]) -> int:
"""Fewest rooms for half-open meetings [start, end). Sorted starts and ends with two pointers: O(n log n)."""
starts = sorted(s for s, _ in meetings)
ends = sorted(e for _, e in meetings)
count = j = 0
for s in starts:
if s >= ends[j]:
j += 1 # the earliest-ending meeting is over: take its room
else:
count += 1
return count
Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/rooms_needed.py
package dev.bekadan.dsa.problems;
import java.util.Arrays;
public final class RoomsNeeded {
private RoomsNeeded() {
}
/** Fewest rooms for half-open meetings {start, end}. Sorted starts and ends with two pointers: O(n log n). */
public static int rooms(int[][] meetings) {
int[] starts = Arrays.stream(meetings).mapToInt(m -> m[0]).sorted().toArray();
int[] ends = Arrays.stream(meetings).mapToInt(m -> m[1]).sorted().toArray();
int rooms = 0;
int j = 0;
for (int s : starts) {
if (s >= ends[j]) {
j++; // the earliest-ending meeting is over: take its room
} else {
rooms++;
}
}
return rooms;
}
}
Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/RoomsNeeded.java
#pragma once
#include <algorithm>
#include <cstddef>
#include <utility>
#include <vector>
namespace bekadan::dsa::problems {
// Fewest rooms for half-open meetings [start, end). Sorted starts and ends with two pointers: O(n log n).
inline int rooms_needed(const std::vector<std::pair<int, int>>& meetings) {
std::vector<int> starts, ends;
for (const auto& [s, e] : meetings) {
starts.push_back(s);
ends.push_back(e);
}
std::ranges::sort(starts);
std::ranges::sort(ends);
int rooms = 0;
std::size_t j = 0;
for (const int s : starts) {
if (s >= ends[j]) {
++j; // the earliest-ending meeting is over: take its room
} else {
++rooms;
}
}
return rooms;
}
} // namespace bekadan::dsa::problems
Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/rooms_needed.hpp