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
Course Schedule III - Greedy Approach INPUT courses[i] = [duration, lastDay] Course 0: [100, 200] 100 days, due day 200 Course 1: [200, 300] 200 days, due day 300 Course 2: [1000, 2000] 1000 days, due day 2000 Timeline Day 1 Day 300 Day 2000 [[100,200],[200,300], [1000,2000]] ALGORITHM STEPS 1 Sort by Deadline Order courses by lastDay [100,200] [200,300] [1000,2000] 2 Use Max-Heap Track taken courses by duration Max-Heap: 200 100 3 Greedy Selection Add course if time allows time=0: Add [100,200] --> time=100 time=100: Add [200,300] --> time=300 time=300: [1000,2000]? 300+1000>2000? NO! 1300 < 2000, can add! 4 Replace if Better If can't fit, check replacement Pop longest if new course shorter Here: All 3 courses fit! Wait... 100+200+1000=1300 OK FINAL RESULT Optimal Course Selection Selected Courses: [100,200] [200,300] Schedule Timeline Course 0 Day 1-100 Course 1 Day 101-300 Course 2 [1000,2000] excluded Adding it: 300+1000=1300 But optimal is max courses=2 OUTPUT 2 OK - Maximum courses taken Key Insight: Sort courses by deadline, then greedily take each course if possible. If a course doesn't fit, replace the longest taken course if the new one is shorter. Max-heap tracks durations efficiently. O(n log n) TutorialsPoint - Course Schedule III | Greedy with Max-Heap Approach
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