Minimum Conference Rooms for Scheduled Meetings
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Tests greedy interval scheduling with heap-based resource allocation to find the smallest number of rooms needed for overlapping meetings.
You are given a list of meeting time slots, where each slot is represented as a pair [start, end] indicating the start and end times of a meeting. Your task is to determine the smallest number of conference rooms needed to accommodate all meetings without any conflicts.
Two meetings can use the same room if one ends exactly when the other begins. In other words, if a meeting ends at time t and another starts at time t, they do not overlap and can share the same room.
Write a function that takes the list of meeting intervals and returns the minimum number of rooms required.
Constraints:
1 <= len(intervals) <= 10^4intervals[i].length == 20 <= start_i < end_i <= 10^6- The input intervals may be given in any order
Examples
Example 1
Input: min_conference_rooms([[0, 30], [5, 10], [15, 20]])
Output: 2
Hints
Hint 1
Sort the meetings by their start times to process them chronologically
Hint 2
Track when each occupied room becomes available using a min-heap of end times
Hint 3
When a meeting starts, check if the earliest-ending room is free for reuse
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is to process meetings in chronological order and track which rooms become available soonest. We use a greedy strategy: whenever a meeting starts, if any room is already free, we reuse it; otherwise, we allocate a new room.
Sort all meetings by start time first. Then maintain a min-heap containing the end times of all currently occupied rooms. For each new meeting:
- Check if the room that finishes earliest (the minimum end time in the heap) is available by the time this meeting starts
- If
earliest_end_time <= meeting_start, that room is free—reuse it by updating its end time to the current meeting's end time - If no room is free, all existing rooms are still occupied, so we must open a new room by adding this meeting's end time to the heap
The heap size at any point represents the number of rooms in use. The final heap size is the answer.
Why the boundary condition matters: A meeting ending at time 10 and another starting at time 10 can share a room because they don't overlap (intervals are effectively half-open). We use <= to capture this.
Correctness: This greedy approach is optimal because we only open a new room when forced to—when all existing rooms are occupied. At that moment, the number of simultaneously running meetings equals the heap size plus one, establishing a lower bound. Since we never open unnecessary rooms, we achieve this lower bound.
Time Complexity: O(n log n) where n is the number of meetings. Sorting takes O(n log n), and each heap operation (push or replace) takes O(log n) with n operations total.
Space Complexity: O(n) in the worst case when all meetings overlap and we need n rooms. The heap stores at most as many entries as there are rooms.
import heapq
from typing import List
def min_conference_rooms(intervals: List[List[int]]) -> int:
if not intervals:
return 0
intervals.sort(key=lambda x: x[0])
room_end_times = []
for start, end in intervals:
if room_end_times and room_end_times[0] <= start:
heapq.heapreplace(room_end_times, end)
else:
heapq.heappush(room_end_times, end)
return len(room_end_times)