LeetCode刷题实战253:会议室II
共 2313字,需浏览 5分钟
·
2021-05-05 07:00
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.
示例
示例 1:
输入: [[0, 30],[5, 10],[15, 20]]
输出: 2
示例 2:
输入: [[7,10],[2,4]]
输出: 1
解题
class Solution {
public:
int minMeetingRooms(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>&b){
return a[0] < b[0];
});
priority_queue<int, vector<int>, greater<int>> heap;
for (int i = 0; i < intervals.size(); i++) {
if (!heap.empty() && heap.top() <= intervals[i][0]) {
heap.pop();
}
heap.push(intervals[i][1]);
}
return heap.size();
}
};