Skip to content

Commit e4ec2da

Browse files
committed
Runtime: 421 ms (Top 69.74%) | Memory: 172.7 MB (Top 82.56%)
1 parent 9f6e240 commit e4ec2da

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1+
// Runtime: 421 ms (Top 69.74%) | Memory: 172.7 MB (Top 82.56%)
12
class Solution {
23
public int[] assignTasks(int[] servers, int[] tasks) {
3-
4+
45
PriorityQueue<int[]> availableServer = new PriorityQueue<int[]>((a, b) -> (a[1] != b[1] ? (a[1] - b[1]) : (a[0] - b[0])));
56
for(int i = 0; i < servers.length; i++){
67
availableServer.add(new int[]{i, servers[i]});
78
}
8-
9-
10-
//int[[] arr,
9+
10+
//int[[] arr,
1111
//arr[0] - server index
1212
//arr[1] - server weight
1313
//arr[2] - free time
1414
PriorityQueue<int[]> processingServer = new PriorityQueue<int[]>(
15-
(a, b) ->
16-
17-
(
18-
a[2] != b[2] ? a[2] - b[2] : // try to sort increasing order of free time
19-
a[1] != b[1] ? a[1] - b[1] : // try to sort increasing order of server weight
20-
a[0] - b[0] // sort increasing order of server index
15+
(a, b) ->
16+
17+
(
18+
a[2] != b[2] ? a[2] - b[2] : // try to sort increasing order of free time
19+
a[1] != b[1] ? a[1] - b[1] : // try to sort increasing order of server weight
20+
a[0] - b[0] // sort increasing order of server index
2121
)
2222
);
23-
24-
23+
2524
int[] result = new int[tasks.length];
26-
25+
2726
for(int i = 0; i < tasks.length; i++){
28-
27+
2928
while(!processingServer.isEmpty() && processingServer.peek()[2] <= i){
3029
int serverIndex = processingServer.remove()[0];
3130
availableServer.add(new int[]{serverIndex, servers[serverIndex]});
3231
}
33-
34-
32+
3533
int currentTaskTimeRequired = tasks[i];
36-
34+
3735
int[] server;
38-
36+
3937
//when current task will free the server done
4038
int freeTime = currentTaskTimeRequired;
41-
39+
4240
if(!availableServer.isEmpty()){
4341
server = availableServer.remove();
4442
freeTime += i;
@@ -47,18 +45,15 @@ public int[] assignTasks(int[] servers, int[] tasks) {
4745
//append previous time
4846
freeTime += server[2];
4947
}
50-
51-
48+
5249
int serverIndex = server[0];
5350
processingServer.add(new int[]{serverIndex, servers[serverIndex] ,freeTime});
54-
55-
51+
5652
//assign this server to current task
5753
result[i] = serverIndex;
5854
}
59-
60-
55+
6156
return result;
62-
57+
6358
}
64-
}
59+
}

0 commit comments

Comments
 (0)