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:
3
💡 Note:
We can take all three courses. Sort by deadline: Course 1 (deadline 200), Course 2 (deadline 300), Course 3 (deadline 2000). Course 1: days 1-100, finishes by day 100 ≤ 200. Course 2: days 101-300, finishes by day 300 ≤ 300. Course 3: days 301-1300, finishes by day 1300 ≤ 2000. All three courses can be completed.
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code