leetcode-华为专题-1353. 最多可以参加的会议数目

时间:2021-08-19
本文章向大家介绍leetcode-华为专题-1353. 最多可以参加的会议数目,主要包括leetcode-华为专题-1353. 最多可以参加的会议数目使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
class Solution {
public:

    static bool cmp(vector<int> &a, vector<int> &b){
        if(a[0]!=b[0])
            return a[0]<b[0];
        else
            return a[1]<b[1];
    } 

    int maxEvents(vector<vector<int>>& events) {
        if(events.size()==0)
            return 0;
        sort(events.begin(), events.end(), cmp);
        priority_queue<int ,vector<int>, greater<int>> minheap;
        vector<int> temp;
        temp = {events[0][0], events[0][1]};
        int cot = 0;
        int j = 0;
        for(int i = 1; i <= 1e5; i++){  // i 代表第几天
            // 将开始时间等于i的结束时间加入堆
            while(j<events.size()&&events[j][0]==i){ 
                // 
                minheap.push(events[j++][1]);  
            }
            // 将结束时间小于今天的,弹出堆
            while(!minheap.empty()&& minheap.top()<i){
                minheap.pop();
            }
            // 当前堆不空,则当前堆顶是结束时间最短的,结果加1,再将堆顶弹出
            if(!minheap.empty()){
                cot++;
                minheap.pop();
            }
        }
        return cot;

    }
};

原文地址:https://www.cnblogs.com/ymec/p/15161203.html