We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b098034 commit 51ada8cCopy full SHA for 51ada8c
C++/course-schedule-iii.cpp
@@ -0,0 +1,23 @@
1
+// Time: O(nlogn)
2
+// Space: O(k), k is the number of courses you can take
3
+
4
+class Solution {
5
+public:
6
+ int scheduleCourse(vector<vector<int>>& courses) {
7
+ sort(courses.begin(), courses.end(),
8
+ [](const vector<int>& a, const vector<int>& b) {
9
+ return a[1] < b[1];
10
+ });
11
+ priority_queue<int> max_heap;
12
+ int now = 0;
13
+ for (const auto& course : courses) {
14
+ max_heap.emplace(course[0]);
15
+ now += course[0];
16
+ if (now > course[1]) {
17
+ now -= max_heap.top(), max_heap.pop();
18
+ }
19
20
+ return heap.size();
21
22
+};
23
0 commit comments