xinyu04

导航

[Oracle] LeetCode 253 Meeting Rooms II 优先队列

Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.

Solution

我们用一个小根堆(优先队列)来保存结束时间,每次比较当前的开始时间和队首元素,如果不重叠,则可以 \(pop\) 出去,代表用了当前的房间,不必添加新的房间

点击查看代码
class Solution {
private:
    int ans=0;
    priority_queue<int,vector<int>,greater<int>> no_use;
public:
    int minMeetingRooms(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end());
        int n = intervals.size();
        no_use.push(intervals[0][1]);
        for(int i=1;i<n;i++){
            if(no_use.top()<=intervals[i][0])no_use.pop();
            no_use.push(intervals[i][1]);
        }
        return no_use.size();
    }
};

posted on 2022-09-25 20:47  Blackzxy  阅读(13)  评论(0编辑  收藏  举报