Advertisement
nikunjsoni

630

May 3rd, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int scheduleCourse(vector<vector<int>>& courses) {
  4.         sort(courses.begin(), courses.end(), [](const vector<int> &a, vector<int> &b){return a[1] < b[1];});
  5.         priority_queue<int> pq;
  6.        
  7.         int totalTime = 0;
  8.         for(auto c: courses){
  9.             if(totalTime + c[0] <= c[1]){
  10.                 totalTime += c[0];
  11.                 pq.push(c[0]);
  12.             }
  13.             else{
  14.                 if(!pq.empty() && pq.top() > c[0]){
  15.                     int lastTime = pq.top();
  16.                     pq.pop();
  17.                     totalTime += (c[0]-lastTime);
  18.                     pq.push(c[0]);
  19.                 }
  20.             }
  21.         }
  22.        
  23.         return pq.size();
  24.     }
  25. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement