nikunjsoni

1882

May 30th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. class Solution {
  2. typedef pair<int, int> T;
  3. public:
  4.     vector<int> assignTasks(vector<int>& servers, vector<int>& tasks) {
  5.         priority_queue<T, vector<T>, greater<T>> fs;
  6.         int n  = servers.size(), m=tasks.size();
  7.         for(int i=0; i<n; i++)
  8.             fs.push({servers[i], i});
  9.        
  10.         priority_queue<T, vector<T>, greater<T>> us;
  11.        
  12.         int j=0, curTime=0;
  13.         vector<int> ans;
  14.         while(j<m){
  15.             curTime = max(j, curTime);
  16.             if(fs.empty())
  17.                 curTime = us.top().first;
  18.             while(!us.empty() && curTime>=us.top().first){
  19.                 auto ser = us.top(); us.pop();
  20.                 fs.push({servers[ser.second], ser.second});
  21.             }
  22.             if(!fs.empty()){
  23.                 auto ser = fs.top(); fs.pop();
  24.                 ans.push_back(ser.second);
  25.                 us.push({curTime+tasks[j], ser.second});
  26.                 j++;
  27.             }
  28.         }
  29.         return ans;
  30.     }
  31. };
Advertisement
Add Comment
Please, Sign In to add comment