Find the smallest number of rooms needed for all meetings.
Start with the concrete trace below. It shows the state the algorithm must carry as it runs.
Problem trace
Meeting Rooms II: Before each start, pop every ended meeting; then push the new end and record the largest heap size.
Input and goalFind the smallest number of rooms needed for all meetings.
Initialize the active heapMeetings are sorted by start as [0,30], [5,10], [5,15], [20,25]. end_times is empty and most_rooms=0.
root -minimum-left -right -
exampleintervals = [[0,30], [5,10], [5,15], [20,25]]heapArray[]heapMeaningroot above its children; active meeting end timesnextMeeting[0,30]mostRooms0
Push end 30At start 0 the heap is empty, so no meeting can be popped. Push 30; heap size 1 raises most_rooms to 1.
root 30minimum-left -right -
exampleintervals = [[0,30], [5,10], [5,15], [20,25]]heapArray[30]heapMeaningroot above its children; active meeting end timesmeeting[0,30]operationpush 30heapSize1mostRooms1
Push end 10At start 5, minimum end 30 <= 5 is false. Push 10, which becomes the min-heap root; most_rooms becomes 2.
root 10minimum-left 30right -
exampleintervals = [[0,30], [5,10], [5,15], [20,25]]heapArray[10,30]heapMeaningroot above its children; active meeting end timesmeeting[5,10]comparison30 <= 5: falseoperationpush 10heapSize2mostRooms2
Push end 15The next meeting also starts at 5. Minimum end 10 <= 5 is false, so push 15; three meetings are active.
root 10minimum-left 30right 15
exampleintervals = [[0,30], [5,10], [5,15], [20,25]]heapArray[10,30,15]heapMeaningroot above its children; active meeting end timesmeeting[5,15]comparison10 <= 5: falseoperationpush 15heapSize3mostRooms3
Pop end 10Before [20,25], minimum end 10 <= start 20 is true. Pop 10; the heap becomes [15,30].
root 15minimum-left 30right -
exampleintervals = [[0,30], [5,10], [5,15], [20,25]]heapArray[15,30]heapMeaningroot above its children; active meeting end timesmeeting[20,25]comparison10 <= 20: trueoperationpop 10heapSize2mostRooms3
Pop end 15The while loop checks again: minimum end 15 <= 20 is true. Pop 15; only end 30 remains active.
root 30minimum-left -right -
exampleintervals = [[0,30], [5,10], [5,15], [20,25]]heapArray[30]heapMeaningroot above its children; active meeting end timesmeeting[20,25]comparison15 <= 20: trueoperationpop 15heapSize1mostRooms3
Stop popping at end 30The next root 30 <= 20 is false, so [0,30] still occupies a room and the cleanup loop stops.
root 30minimum-left -right -
exampleintervals = [[0,30], [5,10], [5,15], [20,25]]heapArray[30]heapMeaningroot above its children; active meeting end timesmeeting[20,25]comparison30 <= 20: falseoperationstop cleanupheapSize1mostRooms3
Push end 25Push the new end 25, which becomes the root above 30. Heap size is 2, so most_rooms stays 3.
root 25minimum-left 30right -
exampleintervals = [[0,30], [5,10], [5,15], [20,25]]heapArray[25,30]heapMeaningroot above its children; active meeting end timesmeeting[20,25]operationpush 25heapSize2updatemax(3, 2) = 3mostRooms3
Return the peak heap sizeThe active heap reached size 3 when [0,30], [5,10], and [5,15] overlapped, so three rooms are necessary and sufficient.
root 25minimum-left 30right -
exampleintervals = [[0,30], [5,10], [5,15], [20,25]]heapArray[25,30]heapMeaningroot above its children; active meeting end timespeakOverlap[0,30], [5,10], [5,15]result3
Recognize it
The question asks for the maximum number of simultaneous intervals, and meetings that end before the next start release reusable capacity.
Keep true
Immediately before pushing a meeting, the heap contains exactly the end times greater than its start; after pushing, heap size equals active rooms and most_rooms is the largest size seen.
Reuse it
For capacity over time, sweep arrivals and keep active expirations ordered by the next one to finish; this transfers to server concurrency, platform usage, and resource reservation.
Read it this way: Meetings are sorted by start as [0,30], [5,10], [5,15], [20,25]. end_times is empty and most_rooms=0. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Pattern: Sort starts and keep active end times in a heap.
Simple idea: Before starting a meeting, remove every meeting that already ended. Add the
new end time. The largest active heap size is the room count.
import heapqdef min_meeting_rooms(intervals: list[list[int]]) -> int: end_times: list[int] = [] most_rooms = 0 for start, end in sorted(intervals): while end_times and end_times[0] <= start: heapq.heappop(end_times) heapq.heappush(end_times, end) most_rooms = max(most_rooms, len(end_times)) return most_rooms