You are given two 0-indexed integer arrays servers and tasks of lengths n and m respectively. servers[i] is the weight of the ith server, and tasks[j] is the time needed to process the jth 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 jth task is inserted into the queue (starting with the 0th 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 jth task will be assigned to.
Input: servers = [3,3,2], tasks = [1,2,3,2,1,2]
Output: [2,2,0,2,1,2]
Explanation: Events in chronological order go as follows:
- At second 0, task 0 is added and processed using server 2 until second 1.
- At second 1, server 2 becomes free. Task 1 is added and processed using server 2 until second 3.
- At second 2, task 2 is added and processed using server 0 until second 5.
- At second 3, server 2 becomes free. Task 3 is added and processed using server 2 until second 5.
- At second 4, task 4 is added and processed using server 1 until second 5.
- At second 5, all servers become free. Task 5 is added and processed using server 2 until second 7.
Example 2:
1
2
3
4
5
6
7
8
9
10
Input: servers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]
Output: [1,4,1,4,1,3,2]
Explanation: Events in chronological order go as follows:
- At second 0, task 0 is added and processed using server 1 until second 2.
- At second 1, task 1 is added and processed using server 4 until second 2.
- At second 2, servers 1 and 4 become free. Task 2 is added and processed using server 1 until second 4.
- At second 3, task 3 is added and processed using server 4 until second 7.
- At second 4, server 1 becomes free. Task 4 is added and processed using server 1 until second 9.
- At second 5, task 5 is added and processed using server 3 until second 7.
- At second 6, task 6 is added and processed using server 2 until second 7.
To efficiently assign tasks to servers based on their weights and availability, we can use two min-heaps: one for free servers and one for busy servers. This allows us to always select the best available server for each task and to quickly update server availability as tasks are processed.
Maintain a min-heap of free servers, ordered by (weight, index). This ensures that the server with the smallest weight (and smallest index in case of a tie) is always chosen first.
Used Servers Heap:
Maintain a min-heap of busy servers, ordered by (availableTime, weight, index). This allows us to efficiently determine when servers become available.
Task Assignment:
For each task (arriving at time i), first move any servers from the used heap to the free heap if they have become available by time i.
If there are free servers, assign the task to the best available server and update its next available time.
If no servers are free, wait for the soonest available server, assign the task as soon as it is free, and update its next available time.
Result:
Track the index of the server assigned to each task in the result array.
import java.util.*;
classSolution {
publicint[]assignTasks(int[] servers, int[] tasks) {
PriorityQueue<int[]> free =new PriorityQueue<>((a, b) -> a[0]!= b[0]? a[0]- b[0] : a[1]- b[1]);
PriorityQueue<int[]> busy =new PriorityQueue<>((a, b) -> a[2]!= b[2]? a[2]- b[2] : (a[0]!= b[0]? a[0]- b[0] : a[1]- b[1]));
int n = servers.length, m = tasks.length;
for (int i = 0; i < n; i++) free.offer(newint[]{servers[i], i, 0});
int[] ans =newint[m];
int time = 0;
for (int i = 0; i < m; i++) {
time = Math.max(time, i);
while (!busy.isEmpty() && busy.peek()[2]<= time) free.offer(busy.poll());
if (free.isEmpty()) {
time = busy.peek()[2];
while (!busy.isEmpty() && busy.peek()[2]== time) free.offer(busy.poll());
}
int[] cur = free.poll();
ans[i]= cur[1];
cur[2]= time + tasks[i];
busy.offer(cur);
}
return ans;
}
}
import heapq
classSolution:
defassignTasks(self, servers, tasks):
n, m = len(servers), len(tasks)
free = [(w, i) for i, w in enumerate(servers)]
heapq.heapify(free)
busy = [] # (availableTime, weight, index) ans = [0] * m
time =0for i, t in enumerate(tasks):
time = max(time, i)
while busy and busy[0][0] <= time:
_, w, idx = heapq.heappop(busy)
heapq.heappush(free, (w, idx))
ifnot free:
time = busy[0][0]
while busy and busy[0][0] == time:
_, w, idx = heapq.heappop(busy)
heapq.heappush(free, (w, idx))
w, idx = heapq.heappop(free)
ans[i] = idx
heapq.heappush(busy, (time + t, w, idx))
return ans
⏰ Time complexity:O((N + M) * log N), where N is the number of servers and M is the number of tasks. Each task assignment and server release involves heap operations.
🧺 Space complexity:O(N + M) for the heaps and result array.