Course Schedule III - Problem

Imagine you're a lifelong learner looking to maximize your knowledge by taking as many online courses as possible! ๐ŸŽ“

You have access to n different online courses, each with specific requirements:

  • Each course i takes exactly duration[i] consecutive days to complete
  • Each course must be finished by its deadline: lastDay[i]
  • You start on day 1 and can only take one course at a time

Your goal is to find the maximum number of courses you can successfully complete within their deadlines.

Example: If you have courses [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]], you need to strategically choose which courses to take to maximize your learning!

Input & Output

example_1.py โ€” Basic Case
$ Input: courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
โ€บ Output: 3
๐Ÿ’ก Note: We can take 3 courses: [100,200], [200,1300], and [2000,3200]. The course [1000,1250] cannot fit because it would make us miss the deadline for [200,1300].
example_2.py โ€” Impossible Course
$ Input: courses = [[1,2]]
โ€บ Output: 1
๐Ÿ’ก Note: We can take this single course since 1 day duration fits within the 2-day deadline.
example_3.py โ€” No Courses Possible
$ Input: courses = [[3,2],[4,3]]
โ€บ Output: 0
๐Ÿ’ก Note: Both courses are impossible to complete - they require more time than their deadlines allow.

Constraints

  • 1 โ‰ค courses.length โ‰ค 104
  • 1 โ‰ค durationi, lastDayi โ‰ค 104
  • courses[i].length == 2

Visualization

Tap to expand
Course Schedule Optimization StrategyAvailable CoursesMathPhysChemBioDuration: [100,1000,200,2000]Sorted by DeadlineMathChemPhysBioDeadline: [200,1250,1300,3200]Max-Heap (Durations)1000200100Timeline Simulation:Day 0Day 200Day 1250Day 1300Day 3200Math (100)Chemistry (200)Physics (1000) - DROPPED!Math Deadline โœ“Chem Deadline โœ“๐ŸŽฏ Key Strategy1. Sort courses by deadline (handle urgent first)2. Use max-heap to track longest courses taken3. Drop longest course when deadline conflicts arise4. This maximizes the total number of courses! ๐Ÿš€
Understanding the Visualization
1
Sort by Urgency
List all courses by their deadlines - handle urgent deadlines first
2
Greedy Selection
Try to take each course, keeping track of total time spent
3
Strategic Dropping
If you can't meet a deadline, drop your most time-consuming course
4
Maximize Learning
This ensures you take the maximum number of courses possible
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach of prioritizing deadlines while strategically dropping the most time-consuming courses ensures we maximize our learning opportunities!
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
95.0K Views
Medium-High Frequency
~25 min Avg. Time
2.8K 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