There are n
different online courses numbered from 1
to n
. You are given an array courses
where courses[i] = [durationi, lastDayi]
indicate that the ith
course should be taken continuously for durationi
days and must be finished before or on lastDayi
.
You will start on the 1st
day and you cannot take two or more courses simultaneously.
Return the maximum number of courses that you can take.
Example 1:
Input: courses = [[100,200],[200,1300],[1000,1250],[2000,3200]] Output: 3 Explanation: There are totally 4 courses, but you can take 3 courses at most: First, take the 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day. Second, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day. Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day. The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.
Example 2:
Input: courses = [[1,2]] Output: 1
Example 3:
Input: courses = [[3,2],[4,3]] Output: 0
Constraints:
1 <= courses.length <= 104
1 <= durationi, lastDayi <= 104
Companies:
Google
Related Topics:
Greedy
Similar Questions:
// OJ: https://leetcode.com/problems/course-schedule-iii/
// Author: github.com/lzl124631x
// Time: O(NT)
// Space: O(NT)
class Solution {
vector<vector<int>> memo;
int dp(vector<vector<int>> &A, int i, int time) {
if (i == A.size()) return 0;
if (memo[i][time] != -1) return memo[i][time];
int pick = 0;
if (time + A[i][0] <= A[i][1]) pick = 1 + dp(A, i + 1, time + A[i][0]);
int skip = dp(A, i + 1, time);
return memo[i][time] = max(pick, skip);
}
public:
int scheduleCourse(vector<vector<int>>& A) {
sort(begin(A), end(A), [](auto &a, auto &b) { return a[1] < b[1]; });
memo.assign(A.size(), vector<int>(A.back()[1] + 1, -1));
return dp(A, 0, 0);
}
};
- We should greedily take the courses whose deadline is earlier.
- If taking a course won't meet its deadline, what we can try is swapping this course with the longest course we've taken that is longer than the current course. In this way, we won't reduce the number of courses we've taken, but at the same time we reduced the total time we used to take courses. That is, this approach
Example: We have 3 courses, [[2,3],[4,6],[3,7]]
- Taking the first course is fine. We spent
2
days and the current deadline is3
. - Taking the second course is fine. We spent
2 + 4 = 6
days and the current deadline is6
. - We can't the third course additionally because it costs
2 + 4 + 3 = 9
days in total but the deadline is7
. What we can do is swapping the 2nd and 3rd courses, so we spent2 + 3 = 5
days in total and the dealine is7
.
// OJ: https://leetcode.com/problems/course-schedule-iii/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
int scheduleCourse(vector<vector<int>>& A) {
sort(begin(A), end(A), [](auto &a, auto &b) { return a[1] == b[1] ? a[0] > b[0] : a[1] < b[1]; });
map<int, int> m;
int time = 1, ans = 0;
for (auto &v : A) {
int duration = v[0], last = v[1];
if (time + duration - 1 <= last) { // if we can take the current course, take it.
++ans;
time += duration;
m[duration]++;
} else if (m.size()) { // if we can't this course, we try swapping this course we've taken previously
int d = m.rbegin()->first; // use the longest course for swapping
if (d > duration) { // the course to be swapped must cost longer time than the current one.
if (--m[d] == 0) m.erase(d);
time += duration - d;
m[duration]++;
}
}
}
return ans;
}
};