hardHeap / Priority QueuePure DSA~35 min
Maximum Tasks Finished Before Their Deadlines
Each task takes a fixed number of days and has a hard deadline (must finish on or before that day). Tasks run one at a time starting from day 1. Maximize how many tasks you complete on time.
Problem
Given an array courses where courses[i] = [duration_i, lastDay_i] (the course takes duration_i days and must be completed by day lastDay_i), and you take courses sequentially starting on day 1, return the maximum number of courses you can take.
Input
An array courses of [duration, lastDay] pairs.
Output
An integer: the maximum number of completable courses.
Constraints
- 1 <= courses.length <= 10000
- 1 <= duration_i, lastDay_i <= 10000
Examples
Example 1
Input
courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
Output
3
Three courses can be scheduled within their deadlines.
Example 2
Input
courses = [[1,2],[2,3]]
Output
2
Both fit: finish the first by day 1, the second by day 3.