leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2402.cpp (1230B)


      1 class Solution {
      2   public:
      3     int mostBooked(int n, vector<vector<int>> &meetings) const {
      4         sort(begin(meetings), end(meetings));
      5 
      6         typedef pair<long long, int> record;
      7         priority_queue<record, vector<record>, greater<>> engaged;
      8         priority_queue<int, vector<int>, greater<>> unused;
      9         vector<int> count(n);
     10 
     11         for (int i = 0; i < n; i++)
     12             unused.push(i);
     13 
     14         for (const auto meeting : meetings) {
     15             const int s = meeting[0], e = meeting[1];
     16 
     17             while (!engaged.empty() && engaged.top().first <= s) {
     18                 unused.push(engaged.top().second);
     19                 engaged.pop();
     20             }
     21 
     22             if (!unused.empty()) {
     23                 const int room = unused.top();
     24                 unused.pop();
     25 
     26                 count[room] += 1;
     27                 engaged.push({e, room});
     28             } else {
     29                 const auto [end, room] = engaged.top();
     30                 engaged.pop();
     31 
     32                 count[room] += 1;
     33                 engaged.push({end + e - s, room});
     34             }
     35         }
     36 
     37         int maxi = 0;
     38         for (int i = 1; i < n; i++) {
     39             if (count[i] > count[maxi]) maxi = i;
     40         }
     41 
     42         return maxi;
     43     }
     44 };