HardArrayGreedyHeap (Priority Queue)Sorting

Course Schedule III

LeetCode
1 approach, code in all languages

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
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now