Course Schedule III - Problem
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.
Key points:
- Each course has a duration and deadline
- Courses must be taken sequentially (no overlap)
- Goal is to maximize the number of courses completed
- You start on day 1
Input & Output
Example 1 — Basic Scheduling
$
Input:
courses = [[100,200],[200,300],[1000,2000]]
›
Output:
2
💡 Note:
We can take courses 1 and 3. Course 1: day 1-100, finishes by day 200. Course 3: day 101-1100, finishes by day 2000. Course 2 would make us exceed its deadline if taken with others.
Example 2 — Single Course
$
Input:
courses = [[1,2]]
›
Output:
1
💡 Note:
Only one course available. Taking it from day 1-1, finishes by day 2, which meets the deadline.
Example 3 — Tight Deadlines
$
Input:
courses = [[3,2],[4,3]]
›
Output:
0
💡 Note:
No courses can be completed. First course needs 3 days but deadline is day 2. Second course needs 4 days but deadline is day 3.
Constraints
- 1 ≤ courses.length ≤ 104
- 1 ≤ durationi, lastDayi ≤ 104
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
Each course has [duration, deadline] - must finish by deadline
2
Key Strategy
Sort by deadline, use heap to track and swap courses optimally
3
Result
Maximum number of courses that can be completed
Key Takeaway
🎯 Key Insight: Sort courses by deadline and use a max heap to optimally swap out the longest course when needed to fit more courses.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code