Course Schedule III LeetCode solution in C++

1
class Solution {
2
public:
3
bool static cmp(vector<int> &a, vector<int> &b) {
4
return a[1] < b[1];
5
}
6
int scheduleCourse(vector<vector<int>> &courses) {
7
sort(courses.begin(), courses.end(), cmp);
8
int sm = 0;
9
priority_queue<int> pq;
10
for (int i = 0; i < courses.size(); i++) {
11
pq.push(courses[i][0]);
12
sm += courses[i][0]; // sum of duration of courses
13
// when there exists a course duration that is invalid and can't be
14
// completed on that day!
15
if (sm > courses[i][1]) {
16
sm -= pq.top(); // remove the biggest invalid course duration!
17
pq.pop();
18
}
19
}
20
return pq.size();
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0