Skip to the code
cf.
+50 XP

#402 Fit the Most Lessons Before Deadlines

hard

A learning app suggests a study plan. Lesson i takes lessons[i][0] days and must be finished by the end of day lessons[i][1]. You study one lesson at a time, starting at day 0, and each lesson's days run back to back. A lesson that starts at day t finishes at day t + duration, and it counts only if that is at most its deadline.

Write mostLessons(lessons) that returns the largest number of lessons you can finish on time, choosing which lessons to take and in what order.

Example 1

Input: lessons = [[50,100],[300,900],[500,800],[1200,2000]]
Output: 3

The lessons due at 100, 800, and 900 finish at days 50, 550, and 850. Taking all four would end at day 2050, past the last deadline 2000.

Example 2

Input: lessons = [[3,2],[4,3]]
Output: 0

Each lesson takes longer than its deadline allows.

Constraints

  • 1 <= lessons.length <= 10^4
  • 1 <= duration, deadline <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).