Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int scheduleCourse(vector<vector<int>>& courses) {
- sort(courses.begin(), courses.end(), [](const vector<int> &a, vector<int> &b){return a[1] < b[1];});
- priority_queue<int> pq;
- int totalTime = 0;
- for(auto c: courses){
- if(totalTime + c[0] <= c[1]){
- totalTime += c[0];
- pq.push(c[0]);
- }
- else{
- if(!pq.empty() && pq.top() > c[0]){
- int lastTime = pq.top();
- pq.pop();
- totalTime += (c[0]-lastTime);
- pq.push(c[0]);
- }
- }
- }
- return pq.size();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement