# C++ solution with comments 19ms

• ``````/**
* Definition for an interval.
* struct Interval {
*     int start;
*     int end;
*     Interval() : start(0), end(0) {}
*     Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
int minMeetingRooms(vector<Interval>& intervals) {
if (intervals.empty()) {
return 0;
}
// Sort according to start time.
sort(intervals.begin(), intervals.end(), [](const Interval &a, Interval &b) {
return a.start < b.start;
});
// Top is min value, heap for the end times.
priority_queue<int, vector<int>, greater<int>> end_times;
end_times.push(intervals[0].end);
for (int i = 1; i < intervals.size(); ++i) {
const int end_t = intervals[i].end;
const int start_t = intervals[i].start;
if (start_t < end_times.top()) {
// Need a new room, as the next start time is earlier than the earliest end time.
end_times.push(end_t);
} else {
// One room is available for the next start meeting, use that room instead.
end_times.pop();
end_times.push(end_t);
}
}
// The size of the queue is the largest room used.
return end_times.size();
}
};
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.