-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_630_CourseScheduleIII.cpp
59 lines (52 loc) · 2.06 KB
/
LC_630_CourseScheduleIII.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
https://leetcode.com/problems/course-schedule-iii/
*/
// Idea is first sort the courses on the basis of course-deadline and then keep track of added courses C
// while processing each course.
// In case, we want to enrol into new course, check if enrolling is possible without removing any course in C.
// If yes,
// then add the course in C
// otherwise
// check if its duration is less than duration of any course in C,
// if yes,
// it means we can remove earlier added course with highest duration and add the current course.
// In doing so, we are also saving some time which can be used for future course. In doing so,
// objective function (maximizing the number of courses in which to enrol) will not change
// but we have get more flexibility in choosing future courses
// :param courses:
class Solution {
public:
static bool compare(vector<int> &a, vector<int>& b){
return a[1] < b[1];
}
int scheduleCourse(vector<vector<int>>& courses) {
int n = courses.size();
priority_queue<int> pq; // a priority queue to maintain the course duration that are taken
int time=0; // total duration of course till current time
//sort according to deadline
// sort(courses.begin(),courses.end(), [&](vector<int> &a, vector<int>& b){ return a[1] < b[1]; });
sort(courses.begin(),courses.end(), compare);
for(const auto &subj: courses)
{
// time += subj[0]; // add current duration
// pq.push(subj[0]);
// if(time > subj[1])
// {
// time -= pq.top();
// pq.pop();
// }
if(time + subj[0] <= subj[1])
{
time += subj[0];
pq.push(subj[0]);
}
else if(!pq.empty() and pq.top()>subj[0])
{
time += subj[0] - pq.top();
pq.pop();
pq.push(subj[0]);
}
}
return pq.size();
}
};