Skip to content

Commit 51ada8c

Browse files
committed
add
1 parent b098034 commit 51ada8c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

C++/course-schedule-iii.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)