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
Course Schedule III: Maximize Course CountInput Courses:Duration: 100Deadline: 200Duration: 200Deadline: 300Duration: 1000Deadline: 2000Timeline Analysis:Day 1 ────────────── Day 2000Must schedule within deadlinesStrategy: Sort by Deadline + Heap OptimizationCourse 1Take: 0→100✓ Fits deadlineCourse 2Skip: Would exceeddeadline with othersCourse 3Take: 100→1100✓ Fits deadlineMaximum Courses: 2Courses 1 and 3 can both be completedOptimal solution uses greedy approach with smart course swapping
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.
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
89.0K Views
Medium-High Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen