Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.4 kB
0
Indexable
Never
#define ll long long
class cmp{
    public:
    bool operator() (const pair<ll,int>&a , const pair<ll,int>&b){
        return a.first>b.first;
    }
};


class Solution {
public:
    int mostBooked(int n, vector<vector<int>>& meet) {
        sort(meet.begin(),meet.end());
        priority_queue<int,vector<int>,greater<int>> av;
        priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>>used; //(end time , room no)
        unordered_map<int,int> freq; // room,freq
        for(int i=0;i<n;i++) av.push(i);

        for(auto &el : meet){
            ll start=el[0], end=el[1];

            //remove finished meetings
            while(!used.empty() && start >=used.top().first){
                av.push(used.top().second);
                used.pop();
            }

            //check availabillity
            if(av.empty()){
                end=used.top().first +(end-start);
                av.push(used.top().second);
                used.pop();
                //roo=used.top.second;
            }

            //start new meetings
            int room=av.top();
            av.pop();
            used.push({end,room});
            freq[room]++;

        }

        int ma = 0;
        for(int i = 1; i < n; i++) {
            if(freq[i] > freq[ma])
                ma = i;
        }
        return ma;
    }
};
Leave a Comment