Skip to the code
+25 XP

#749 Completion Order of Batched Requests

medium

A dashboard loads its widgets' data with at most limit requests in flight, starting requests in index order (the first limit at time 0, then each next one as soon as any request finishes). Request i takes durations[i] ms.

Widgets render as soon as their data arrives. Write completionOrder(durations, limit) that returns the request indexes in the order they finish. Requests that finish at the same time are listed by index, smallest first.

A dashboard can make up to 100,000 requests, so find the order without stepping through every millisecond or comparing every request with every other one.

Example 1

Input: durations = [300,100,200,100,400], limit = 2
Output: [1,0,2,3,4]

Finish times are 300, 100, 300, 400, 700. Requests 0 and 2 tie at 300, so 0 comes first.

Example 2

Input: durations = [5,4,3], limit = 3
Output: [2,1,0]

All start together, so the shortest finishes first.

Constraints

  • 0 <= durations.length <= 10^5, 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 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).