-
Notifications
You must be signed in to change notification settings - Fork 0
/
q2.cpp
37 lines (33 loc) · 918 Bytes
/
q2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Given the solution class only, as required on the question link.
class Solution
{
public:
static bool sortcol(const vector<int> &v1, const vector<int> &v2)
{
return v1[1] < v2[1];
}
int scheduleCourse(vector<vector<int>> &courses)
{
int n = courses.size();
sort(courses.begin(), courses.end(), sortcol);
// for(int i=0;i<n;i++){
// cout << courses[i][0] << " " << courses[i][1] << endl;
// }
int currTime = 0;
int totalCourses = 0;
priority_queue<int> q;
for (int i = 0; i < n; i++)
{
q.push(courses[i][0]);
currTime += courses[i][0];
totalCourses++;
if (currTime > courses[i][1])
{
currTime -= q.top();
totalCourses--;
q.pop();
}
}
return totalCourses;
}
};