There are n different online courses numbered from 1 to n. You are given an array courses where courses[i] = [duration_i, lastDay_i] indicates that the i-th course should be taken continuously for duration_i days and must be finished before or on lastDay_i.
You start on day 1 and you cannot take two or more courses simultaneously.
Return the maximum number of courses that you can take.
Function Signature
def max_courses(courses: list[list[int]]) -> int:
Rules
-
Each course is taken at most once, and the chosen courses may be taken in any order.
-
A course of duration
d
that starts on day
s
occupies days
s
through
s + d - 1
, and it counts only if
s + d - 1 <= lastDay
. Finishing exactly on the last day is allowed.
-
The next course can start the day after the previous one ends.
Constraints
-
1 <= courses.length <= 10^4
-
1 <= duration_i, lastDay_i <= 10^4
Examples
Input: courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
Output: 3
Take the 1st course (days 1-100), then the 3rd course (days 101-1100), then the 2nd course (days 1101-1300). All four courses together need 3,300 days, which is past the latest last day of 3,200, so 4 is impossible.
Input: courses = [[5,5],[4,6],[2,6]]
Output: 2
Take [4,6] on days 1-4 and [2,6] on days 5-6. All three together need 11 days while no last day is later than 6, so 3 is impossible.
Input: courses = [[3,2],[4,3]]
Output: 0
Each course takes longer than its own last day allows, even when started on day 1, so neither can be taken.