HardArrayGreedyHeap (Priority Queue)Sorting

Course Schedule III

1 approach, code in every language

Problem description

You are given a list of online courses, where each course is described by two integers: the number of days it takes to finish and the last day by which it must be completed. Specifically course i is written as [duration_i, lastDay_i], meaning it occupies duration_i consecutive days and has to be finished on or before day lastDay_i.

You start on the first day and can take at most one course at a time; courses are taken one after another with no overlap. A course counts as completed only if it finishes on or before its deadline.

Return the maximum number of courses you can complete under these rules.

Example 1

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

Output: 3

Taking the first three courses in a feasible order finishes them within their deadlines; the last course cannot fit, so the answer is three.

Example 2

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

Output: 0

Neither course can finish by its deadline, since the first needs 3 days but is due on day 2, so no courses are completed.

Constraints

  • 1 <= courses.length <= 10^4
  • 1 <= duration_i, lastDay_i <= 10^4

After the pattern

Now solve one you haven't seen.

Reading a pattern isn't the same as using it. The practice set has 1,194 original problems on the same techniques, each graded in your browser.

1,194 problems · 15,062 graded tests · free