The Course Schedule III interview question is significantly different from the first two. You are given a list of courses, where each course has a duration and a lastDay (deadline). You can only take one course at a time, and a course must be completed by its lastDay. Your goal is to find the maximum number of courses you can take.
Amazon and Salesforce use the Course Schedule III coding problem to test a candidate's mastery of Greedy algorithms and Heaps (Priority Queues). Unlike the previous versions which were about connectivity, this is about scheduling and resource allocation. It evaluates if you can identify a strategy that minimizes "regret" by swapping long courses for shorter ones to fit more courses into the schedule.
This problem is solved using a Greedy approach with a Max-Heap.
lastDay (deadlines). This ensures you process courses in the order they must be finished.currentTime.currentTime. Push the duration into a Max-Heap.currentTime exceeds the current course's lastDay, remove the longest course seen so far from the heap (the top of the max-heap) and subtract its duration from currentTime.
This ensures that at any point, you have taken the maximum possible courses within the time limit by always discarding the most time-consuming ones when a conflict occurs.Courses: [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]]
[100, 200], [1000, 1250], [200, 1300], [2000, 3200].currentTime = 100. (Valid). Heap: [100].currentTime = 1100. (Valid, 1100 < 1250). Heap: [1000, 100].currentTime = 1300. (Valid, 1300 <= 1300). Heap: [1000, 200, 100].currentTime = 3300. (Invalid, 3300 > 3200).
currentTime = 3300 - 1000 = 2300.[2000, 200, 100].
Result: 3 courses.This is a classic "Interval Scheduling with Regret" problem. Whenever you need to fit the "most" items into a fixed space/time and you can swap items, a Greedy approach with a Priority Queue is a strong candidate.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Put Marbles in Bags | Hard | Solve | |
| Maximum Performance of a Team | Hard | Solve | |
| IPO | Hard | Solve | |
| Minimum Cost to Hire K Workers | Hard | Solve | |
| Maximum Subsequence Score | Medium | Solve |