Skip to the code
cf.
+50 XP

#750 Smallest Limit That Meets a Deadline

hard

A build pipeline runs jobs with a concurrency limit: jobs start in index order, the first limit at time 0, and each next job starts the moment any running job finishes. Job i takes durations[i] seconds. More parallel workers cost money, so the team wants the smallest limit that still finishes every job within deadline seconds.

Write smallestLimit(durations, deadline) that returns the smallest limit from 1 to durations.length for which the last job finishes at or before deadline, or -1 if even running every job at once is too slow. For this way of starting jobs, a bigger limit never makes the finish time later.

Example 1

Input: durations = [300,100,200,100,400], deadline = 700
Output: 2

With a limit of 1 the jobs take 1100 seconds; with 2 they finish at 700, just in time.

Example 2

Input: durations = [50,60], deadline = 40
Output: -1

Even running both at once takes 60 seconds.

Constraints

  • 1 <= durations.length <= 10^4, 1 <= durations[i] <= 10^4
  • 1 <= deadline <= 10^9
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).