Given intervals where intervals[i] = [startα΅’, endα΅’], return the minimum number of conference rooms required to hold all meetings.
Example
Input: intervals = [[0,30],[5,10],[15,20]]
Output:2// [0,30] conflicts with [5,10] β need 2 rooms. [15,20] fits in room freed by [5,10].
Constraints
β’ 1 β€ intervals.length β€ 10β΄
02
Section Two Β· Approach 1
Sweep Line β O(n log n)
Separate all start times (+1 event) and end times (-1 event). Sort events. Sweep through, tracking running count. The peak count is the answer.
Why it works
Each start adds a concurrent meeting, each end removes one. The maximum concurrency = rooms needed.
Java β Sweep line
import java.util.*;
classSolution {
publicintminMeetingRooms(int[][] intervals) {
int[] starts = newint[intervals.length];
int[] ends = newint[intervals.length];
for (int i = 0; i < intervals.length; i++) {
starts[i] = intervals[i][0];
ends[i] = intervals[i][1];
}
Arrays.sort(starts);
Arrays.sort(ends);
int rooms = 0, e = 0;
for (int s : starts) {
if (s < ends[e]) rooms++; // new room neededelse e++; // reuse freed room
}
return rooms;
}
}
Python β Sweep line
classSolution:
defminMeetingRooms(self, intervals: list[list[int]]) -> int:
starts = sorted(s for s, e in intervals)
ends = sorted(e for s, e in intervals)
rooms = ep = 0for s in starts:
if s < ends[ep]: rooms += 1else: ep += 1return rooms
03
Section Three Β· Approach 2
Min-Heap β O(n log n)
Sort by start. Use a min-heap of end times (earliest room to free). For each meeting: if its start β₯ heap's min β reuse that room (poll + push new end). Else push new end (new room).
π‘ Mental model: Imagine you're a receptionist. You have a list of meetings sorted by start time and a priority board showing when each room frees up (min-heap). For each meeting: peek at the board β if the earliest-freeing room is free by now, assign that room (update its end time). Otherwise, open a new room. The heap size = rooms in use.
Algorithm
Sort intervals by start.
Min-heap of end times. Push first meeting's end.
For each subsequent meeting: if start β₯ heap.peek() β heap.poll() (reuse room).
Push current meeting's end. Heap size = rooms in use.
Return max heap size (= heap.size() at end).
π― When to recognize this pattern: "How many resources needed concurrently?" (rooms, servers, threads). Two classic approaches: sweep line (sort events) and min-heap (track active resource end times). Both are O(n log n). Sweep line is simpler; min-heap is more intuitive for "room allocation" reasoning.
04
Section Four Β· Trace
Visual Walkthrough
Trace for [[0,30],[5,10],[15,20]] using sweep line.
Meeting Rooms II β Sweep Line
05
Section Five Β· Implementation
Code β Java & Python (Min-Heap)
Java β Min-heap
import java.util.*;
classSolution {
publicintminMeetingRooms(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
PriorityQueue<Integer> heap = newPriorityQueue<>();
for (int[] iv : intervals) {
if (!heap.isEmpty() && iv[0] >= heap.peek())
heap.poll(); // reuse room
heap.offer(iv[1]); // occupy room until end
}
return heap.size();
}
}
Python β Min-heap
import heapq
classSolution:
defminMeetingRooms(self, intervals: list[list[int]]) -> int:
intervals.sort()
heap = []
for s, e in intervals:
if heap and s >= heap[0]:
heapq.heapreplace(heap, e) # reuse roomelse:
heapq.heappush(heap, e) # new roomreturn len(heap)
06
Section Six Β· Analysis
Complexity Analysis
Approach
Time
Space
Trade-off
Sweep line
O(n log n)
O(n)
Two sorted arrays; two pointers
Min-heap
O(n log n)
O(n)
Heap tracks active rooms; more intuitive for allocation
07
Section Seven Β· Edge Cases
Edge Cases & Pitfalls
Case
Input
Why It Matters
One meeting
[[1,5]]
1 room.
No overlap
[[1,2],[3,4]]
1 room.
All overlap
[[1,5],[2,6],[3,7]]
3 rooms (all concurrent at time 3).
Touching
[[1,5],[5,10]]
start=5 β₯ end=5 β reuse. 1 room.
β Common Mistake: In the sweep-line approach, using s β€ ends[e] instead of s < ends[e]. If a meeting starts exactly when another ends, they don't overlap β the room is free. Use strict less-than to require a new room only when the meeting truly overlaps.