Course Schedule III

You are given courses where each course is represented by [duration, lastDay]. You start on day 1 and can take at most one course at a time. Return the maximum number of courses you can take. Pattern focus: Greedy Scheduling. Sort by deadline, take each course if possible, and when you exceed a deadline, drop the course with the largest duration from the current set.

Input Format

courses = list of [duration, lastDay] pairs

Output Format

maximum number of courses that can be completed

Constraints

  • 1 <= courses.length <= 10^5
  • 1 <= duration, lastDay <= 10^9

Examples

Example 1:

Input:

courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]

Output:

3

Explanation:

You can complete three courses by keeping the shortest valid durations before each deadline.

Example 2:

Input:

courses = [[1,2],[2,3]]

Output:

2

Explanation:

Both courses can be completed before their deadlines.

Example 3:

Input:

courses = [[3,2],[4,3]]

Output:

0

Explanation:

Neither course can be finished before its deadline.

Loading...
Course Schedule III - Greedy DSA Problem