Process Tasks Using Servers - Problem

You are given two 0-indexed integer arrays servers and tasks of lengths n and m respectively. servers[i] is the weight of the i-th server, and tasks[j] is the time needed to process the j-th task in seconds.

Tasks are assigned to the servers using a task queue. Initially, all servers are free, and the queue is empty.

At second j, the j-th task is inserted into the queue (starting with the 0-th task being inserted at second 0). As long as there are free servers and the queue is not empty, the task in the front of the queue will be assigned to a free server with the smallest weight, and in case of a tie, it is assigned to a free server with the smallest index.

If there are no free servers and the queue is not empty, we wait until a server becomes free and immediately assign the next task. If multiple servers become free at the same time, then multiple tasks from the queue will be assigned in order of insertion following the weight and index priorities above.

A server that is assigned task j at second t will be free again at second t + tasks[j].

Build an array ans of length m, where ans[j] is the index of the server the j-th task will be assigned to.

Return the array ans.

Input & Output

Example 1 — Basic Server Assignment
$ Input: servers = [3,3,2], tasks = [1,2,3,2,1,2]
Output: [2,2,0,2,1,2]
💡 Note: Task 0 (time=1) → Server 2 (weight=2, smallest). Task 1 arrives at t=1, Server 2 busy until t=2, so assign to Server 0 (weight=3, index=0). Continue this process for all tasks.
Example 2 — Equal Weight Priority
$ Input: servers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]
Output: [1,4,1,4,1,3,2]
💡 Note: Server 1 has smallest weight (1), so gets first task. When multiple servers are free, choose by smallest weight then smallest index.
Example 3 — Waiting for Servers
$ Input: servers = [10,20], tasks = [5,3,4]
Output: [0,1,0]
💡 Note: Task 0 → Server 0. Task 1 arrives at t=1, both servers busy, wait until Server 0 finishes at t=5. Task 2 waits for next available server.

Constraints

  • 1 ≤ servers.length, tasks.length ≤ 2 × 105
  • 1 ≤ servers[i], tasks[i] ≤ 2 × 105

Visualization

Tap to expand
Server Task Assignment ProcessServers:Weight: 3Index: 0Weight: 3Index: 1Weight: 2Index: 2Tasks:123Assignment Logic:1. Task 0 → Server 2 (lowest weight: 2)2. Task 1 → Server 2 busy, choose Server 03. Task 2 → Wait for earliest free serverBest choicePriority: Smallest Weight → Smallest IndexOutput: [2, 0, 2] (server indices)
Understanding the Visualization
1
Input
Servers with weights [3,3,2] and tasks with durations [1,2,3]
2
Process
Assign each task to available server with smallest weight/index
3
Output
Array of server indices that handled each task
Key Takeaway
🎯 Key Insight: Use priority queues to efficiently find the optimal server without scanning all servers for each task
Asked in
Amazon 15 Microsoft 12 Google 8 Meta 6
23.4K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen