Skip to the code
cf.
+50 XP

#401 Assign Uploads to Servers

hard

A media service has upload servers with different weights, servers[i]. Upload j becomes ready at time j and takes uploads[j] time units. Uploads are assigned in order of j, one server each.

When upload j is assigned, it takes the free server with the smallest weight (smallest index on ties). If no server is free, it waits until the earliest moment some server becomes free, and then takes the best server among those free at that moment. A server that finishes at time t is free at time t. An upload can never start before the upload assigned just before it.

Write assignUploads(servers, uploads) that returns, for each upload, the index of the server it uses.

Example 1

Input: servers = [4,1,3], uploads = [2,1,3,1]
Output: [1,2,1,2]

Server 1 is lightest but busy at time 1, so upload 1 takes server 2. At time 2 both are free again: upload 2 takes server 1 and upload 3 takes server 2.

Example 2

Input: servers = [2,1], uploads = [4,4,1]
Output: [1,0,1]

Both servers are busy at time 2, so upload 2 waits until time 4, when server 1 becomes free.

Constraints

  • 1 <= servers.length, uploads.length <= 2 * 10^5
  • 1 <= servers[i], uploads[j] <= 2 * 10^5
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).