Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit533cbb7

Browse files
authored
Merge pull requestneetcode-gh#2497 from leahjia/1882-process-tasks-using-servers
Create 1882-process-tasks-using-servers.java
2 parents2a2fb2f +d9127ec commit533cbb7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
classSolution {
2+
publicint[]assignTasks(int[]servers,int[]tasks) {
3+
int[]res =newint[tasks.length];
4+
5+
// [weight, index]
6+
Queue<int[]>available =newPriorityQueue<>((a,b) ->a[0] ==b[0] ?a[1] -b[1] :a[0] -b[0]);
7+
for (inti =0;i <servers.length;i++)
8+
available.add(newint[] {servers[i],i });
9+
10+
inttime =0;
11+
intnextTask =0;
12+
// [weight, index, done time]
13+
Queue<int[]>unavail =newPriorityQueue<>((a,b) ->a[2] -b[2]);
14+
while (nextTask <tasks.length) {
15+
// release available servers
16+
while (!unavail.isEmpty() &&unavail.peek()[2] <=time) {
17+
int[]curr =unavail.poll();
18+
available.add(newint[] {curr[0],curr[1] });
19+
}
20+
21+
// assign task(s)
22+
while (!available.isEmpty() &&nextTask <time &&nextTask !=tasks.length) {
23+
int[]curr =available.poll();
24+
unavail.add(newint[] {curr[0],curr[1],time +tasks[nextTask] });
25+
res[nextTask++] =curr[1];
26+
}
27+
28+
// advance time
29+
if (available.isEmpty())
30+
time =unavail.peek()[2];
31+
else
32+
time++;
33+
}
34+
returnres;
35+
}
36+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp