Given an array of meeting time intervals intervals
where intervals[i] = [starti, endi]
, return the minimum number of conference rooms required.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]] Output: 2
Example 2:
Input: intervals = [[7,10],[2,4]] Output: 1
Constraints:
1 <= intervals.length <= 104
0 <= starti < endi <= 106
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int minMeetingRooms(vector<vector<int>>& intervals) { | |
// sort by start time | |
sort(intervals.begin(), intervals.end(), [&](vector<int> x, vector<int> y){ | |
return x[0] < y[0]; | |
}); | |
// keep end times | |
priority_queue<int, vector<int>, greater<>> min_heap; | |
int n = intervals.size(); | |
for(int i = 0; i < n; i++){ | |
int start = intervals[i][0]; | |
int end = intervals[i][1]; | |
// use existing room (means have to remove) | |
if(!min_heap.empty() and start >= min_heap.top()){ | |
min_heap.pop(); | |
} | |
// add new room | |
min_heap.push(end); | |
} | |
return min_heap.size(); | |
} | |
}; |
No comments:
Post a Comment