Minimum Concurrent Resources for Overlapping Intervals
Greedy & IntervalsAuto-graded · 20 test casesLast updated
Find the maximum number of overlapping intervals at any instant using sweep line or heap techniques.
Given a collection of meeting time intervals, determine the minimum number of rooms needed to accommodate all meetings without any room hosting overlapping meetings.
Each meeting is represented as a tuple of integers (start, end), corresponding to the half-open interval [start, end).
Rules
- A room can host at most one meeting at any given instant.
- A meeting occupies a room strictly from
startup to, but not including,end. - If one meeting ends at time $t$ and another starts at time $t$, they do not overlap and may share the same room.
- Duplicate meeting intervals are allowed; each occurrence represents an independent meeting requiring a room.
- If no meetings are provided, return
0.
Constraints
- $0 \le \text{len(meetings)} \le 10^5$
- $0 \le \text{start} < \text{end} \le 10^9$ for each meeting
Examples
Example 1
Input: min_rooms([[1, 5], [2, 6], [4, 8], [6, 9]])
Output: 3
Example 2
Input: min_rooms([[1, 3], [3, 5], [5, 7]])
Output: 1
Example 3
Input: min_rooms([[10, 20], [10, 20], [20, 30]])
Output: 2
Hints
Hint 1
The minimum number of rooms required equals the maximum number of meetings occurring concurrently at any point in time.
Hint 2
Consider transforming each meeting into two discrete events: a room becoming occupied (+1) and a room becoming available (-1).
Hint 3
Pay close attention to how events occurring at the exact same timestamp are sorted to respect the half-open interval rule [start, end).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach: Line Sweep Algorithm
The problem asks for the minimum number of rooms needed such that no two meetings in the same room overlap. This is equivalent to finding the maximum number of overlapping intervals at any single moment.
We can solve this efficiently using a sweep-line technique:
- Decompose each meeting
(start, end)into two boundary events:(start, +1)indicating a meeting begins and requests a room.(end, -1)indicating a meeting finishes and releases its room.
- Sort all events primarily by time in ascending order.
- For tie-breaking when two events share the same timestamp, process room releases (
-1) before room acquisitions (+1). This models the half-open interval property[start, end)correctly—a room freed at time $t$ is instantly available for a meeting starting at time $t$. - Iterate through the sorted events, maintaining a running total of active rooms and tracking the maximum peak reached.
Complexity
- Time Complexity: $\mathcal{O}(N \log N)$ where $N$ is the total number of meetings. Generating $2N$ events takes $\mathcal{O}(N)$ time, sorting takes $\mathcal{O}(N \log N)$ time, and the linear sweep takes $\mathcal{O}(N)$ time.
- Space Complexity: $\mathcal{O}(N)$ to store the event records.
Reference Solution
def min_rooms(meetings: list[tuple[int, int]]) -> int:
events = []
for start, end in meetings:
events.append((start, 1))
events.append((end, -1))
# Primary sort by time ascending.
# Secondary sort puts -1 before +1 to handle half-open interval boundaries [start, end).
events.sort(key=lambda x: (x[0], x[1]))
current_rooms = 0
max_rooms = 0
for _, change in events:
current_rooms += change
if current_rooms > max_rooms:
max_rooms = current_rooms
return max_rooms