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.
A pipeline can have 100,000 jobs, so neither trying every limit nor checking each limit with a slow simulation finishes in time.
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.
Input: durations = [50,60], deadline = 40 Output: -1
Even running both at once takes 60 seconds.
1 <= durations.length <= 10^5, 1 <= durations[i] <= 10^41 <= deadline <= 10^9Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #750 Smallest Limit That Meets a Deadline. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.