Advertisement
pb_jiang

leetcode2402

Sep 4th, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. class Solution {
  2.     using ll = long long;
  3.     vector<ll> mend;
  4.     ll find_room_before(ll time) {
  5.         ll ret = -1;
  6.         auto me = min_element(mend.begin(), mend.end());
  7.         if (*me > time) {
  8.             return me - mend.begin();
  9.         }
  10.         for (int i = 0; i < mend.size(); ++i) {
  11.             if (mend[i] <= time && ret == -1) {
  12.                 ret = i;
  13.                 break;
  14.             }
  15.         }
  16.         return ret;
  17.     }
  18. public:
  19.     int mostBooked(int n, vector<vector<int>>& mt) {
  20.         sort(mt.begin(), mt.end());
  21.         vector<ll> mcnt(n);
  22.         mend = vector<ll>(n, -1);
  23.  
  24.         for (auto meeting: mt) {
  25.             ll rid = find_room_before(meeting[0]);
  26.             mcnt[rid]++;
  27.             ll end = max(mend[rid], (ll)meeting[0]) + meeting[1] - meeting[0];
  28.             mend[rid] = end;
  29.         }
  30.         ll ret = 0, max_mt = mcnt[0];
  31.         for (int i = 1; i < mcnt.size(); ++i) {
  32.             if (mcnt[i] > max_mt) {
  33.                 max_mt = mcnt[i];
  34.                 ret = i;
  35.             }
  36.         }
  37.         return ret;
  38.     }
  39. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement