</>

253. Meeting Rooms II

Medium NeetCode 150
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Intervals
Time
—
Space
—

Standard Solution (C++)

SummaryGiven a list of meeting time intervals, find the minimum number of conference rooms needed so that all meetings can be held simultaneously without conflicts.

Approach
  • Sort intervals by start time, then use a min-heap tracking the earliest end time among all rooms currently in use.
  • For each meeting, if the earliest-ending room frees up before (or exactly when) this meeting starts, reuse that room by replacing its end time; otherwise, allocate a new room.
  • The heap size at the end equals the peak number of simultaneously active meetings, which is the minimum rooms required.
  • This greedy strategy works because greedily reusing the room that becomes free earliest minimizes the total room count.
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

class Solution {
public:
    int minMeetingRooms(vector<vector<int>>& intervals) {
        // Sort meetings by start time
        sort(intervals.begin(), intervals.end());

        // Min-heap storing end times of ongoing meetings
        priority_queue<int, vector<int>, greater<int>> minHeap;

        for (const auto& interval : intervals) {
            int start = interval[0], end = interval[1];
            // If the earliest-ending room is free, reuse it
            if (!minHeap.empty() && minHeap.top() <= start) {
                minHeap.pop();
            }
            // Assign this meeting to a room (new or reused)
            minHeap.push(end);
        }

        // Each element in the heap represents an occupied room
        return static_cast<int>(minHeap.size());
    }
};
Time O(n log n) Space O(n)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

#include <vector>
#include <queue>

using namespace std;

class Solution {
public:
    int minMeetingRooms(vector<vector<int>>& intervals) {
        int count=0;
        int n=intervals.size();
        std::priority_queue<int, vector<int>, greater<int>> pq;
        pq.push(0);

        sort(intervals.begin(), intervals.end());
        for(const auto& i: intervals){
            int curMin=pq.top();
            if(curMin<=i[0]){
                pq.pop();
                pq.push(i[1]);
            }
            else{
                pq.push(i[1]);
            }
        }
        
        return pq.size();
    }
};

// Time: O(nlogn)
// Space: O(n) in the worst case when all meetings overlap, otherwise O(k) where k is the maximum number of overlapping meetings.

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern