Course Schedule III Problem
Description
LeetCode Problem 630.
There are n different online courses numbered from 1 to n. You are given an array courses where courses[i] = [duration_i, lastDay_i] indicate that the i^th course should be taken continuously for duration_i days and must be finished before or on lastDay_i.
You will start on the 1^st 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 1^st course, it costs 100 days so you will finish it on the 100^th day, and ready to take the next course on the 101^st day.
Second, take the 3^rd course, it costs 1000 days so you will finish it on the 1100^th day, and ready to take the next course on the 1101^st day.
Third, take the 2^nd course, it costs 200 days so you will finish it on the 1300^th day.
The 4^th course cannot be taken now, since you will finish it on the 3300^th 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 <= 10^4
- 1 <= duration_i, lastDay_i <= 10^4
Solution
Approach: Greedy by Deadline with a Max-Heap
Order. If a set of courses can all be finished in time, they can also be finished by taking them in order of lastDay: swapping two neighbors that are out of deadline order never makes the later one miss its deadline. So sort the courses by lastDay and only decide which courses to keep.
Choice. Go through the sorted courses, keeping the courses chosen so far and day, the total of their durations (the day the last one finishes):
- Add the current course: push its duration onto a max-heap and add it to
day. - If
day > lastDay, the set no longer fits. Remove the longest course in the heap (it may be the one just added) and subtract its duration fromday. - The answer is the size of the heap at the end.
Why removing the longest is right: we end up with as many courses as before this step, and with the smallest possible day for that count, which can only help later courses. The kept set still fits: the courses before the removed one are unchanged, the ones after it move earlier, and the new total oldDay + duration - longest is at most oldDay (because longest ≥ duration). oldDay met an earlier deadline, so it meets this one too.
Example 1, sorted by deadline: [100,200], [1000,1250], [200,1300], [2000,3200]. After the first three, day = 1300, which fits. Adding 2000 gives day = 3300 > 3200, so drop 2000 itself; three courses remain.
Complexity: O(n log n) time for the sort and the heap operations, and O(n) space for the heap, where n is the number of courses.
import heapq
class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
courses.sort(key=lambda course: course[1]) # earliest deadline first
taken = [] # durations of the courses we keep, as a max-heap (negated values)
day = 0 # total days used by the courses we keep
for duration, last_day in courses:
heapq.heappush(taken, -duration)
day += duration
if day > last_day:
# Too late: drop the longest course kept so far (maybe this one).
day += heapq.heappop(taken) # the popped value is minus that duration
return len(taken)
Now write it from memory
Close the write-up and solve it yourself against the tests.