-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy path1882-process-tasks-using-servers.java
36 lines (32 loc) · 1.26 KB
/
1882-process-tasks-using-servers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public int[] assignTasks(int[] servers, int[] tasks) {
int[] res = new int[tasks.length];
// [weight, index]
Queue<int[]> available = new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
for (int i = 0; i < servers.length; i++)
available.add(new int[] { servers[i], i });
int time = 0;
int nextTask = 0;
// [weight, index, done time]
Queue<int[]> unavail = new PriorityQueue<>((a, b) -> a[2] - b[2]);
while (nextTask < tasks.length) {
// release available servers
while (!unavail.isEmpty() && unavail.peek()[2] <= time) {
int[] curr = unavail.poll();
available.add(new int[] { curr[0], curr[1] });
}
// assign task(s)
while (!available.isEmpty() && nextTask < time && nextTask != tasks.length) {
int[] curr = available.poll();
unavail.add(new int[] { curr[0], curr[1], time + tasks[nextTask] });
res[nextTask++] = curr[1];
}
// advance time
if (available.isEmpty())
time = unavail.peek()[2];
else
time++;
}
return res;
}
}