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
Process Tasks Using Servers INPUT servers = [3, 3, 2] 3 idx 0 3 idx 1 2 idx 2 tasks = [1,2,3,2,1,2] 1 t0 2 t1 3 t2 2 t3 1 t4 2 t5 Server Visualization: S0:w3 S1:w3 S2:w2 Weight 2 is smallest ALGORITHM STEPS 1 Two Min-Heaps Free: (weight, idx) Busy: (freeTime, weight, idx) 2 Process at time t Move servers from Busy to Free if freeTime <= t 3 Assign Task Pop min from Free heap Add to Busy with new time 4 Handle Wait If Free empty, jump to next available time Heap Structure: Free Heap (w,idx) sorted Busy Heap (time,w,idx) Priority: smallest first FINAL RESULT Execution Trace: t=0: task0 --> S2 (w=2) t=1: task1 --> S2 free at 1 t=2: task2 --> S0 (w=3,i=0) t=3: task3 --> S2 free at 3 t=4: task4 --> S1 (w=3,i=1) t=5: task5 --> S2 free at 5 Server Timeline: S0 t2 S1 t4 S2 0 2 4 6 Output: [2, 2, 0, 2, 1, 2] OK - All tasks assigned! S2 handles 4 tasks (lowest weight) Key Insight: Use two min-heaps: Free servers sorted by (weight, index) and Busy servers sorted by (freeTime, weight, index). This ensures O(log n) operations for finding the best server. When no server is free, skip time to the next available moment. Time complexity: O(m log n) where m = tasks, n = servers. TutorialsPoint - Process Tasks Using Servers | Two Min-Heaps Approach
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