Skip to the code
cf.
+10 XP

#747 When Does the Prefetch Finish?

easy

A gallery prefetches images with at most limit downloads at once. Download i takes durations[i] ms. Downloads start in index order: at time 0 the first limit start, and whenever one finishes, the next waiting download starts at that moment.

Write prefetchTime(durations, limit) that returns the time when the last download finishes, or 0 if there are none.

Example 1

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

Tasks 0 and 1 start at 0; 2 starts at 100; 3 and 4 start at 300. Task 4 ends at 300 + 400.

Example 2

Input: durations = [50,50,50], limit = 5
Output: 50

Everything runs at once.

Constraints

  • 0 <= durations.length <= 10^3, 1 <= durations[i] <= 10^4
  • 1 <= limit <= 100
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).