Skip to content

Commit 88711de

Browse files
authored
Create 1882-process-tasks-using-servers.kt
1 parent 9a2a4e5 commit 88711de

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
fun assignTasks(servers: IntArray, tasks: IntArray): IntArray {
3+
val res = IntArray (tasks.size)
4+
5+
val available = PriorityQueue<IntArray>(compareBy ({ it[0] }, { it[1] })).apply {
6+
for ((index, weight) in servers.withIndex())
7+
this.add(intArrayOf(weight, index))
8+
}
9+
10+
val unAvailable = PriorityQueue<IntArray>(compareBy { it[0] })
11+
12+
var t = 0
13+
for (i in tasks.indices) {
14+
t = maxOf(t, i)
15+
16+
if (available.isEmpty())
17+
t = unAvailable.peek()[0]
18+
19+
while (unAvailable.isNotEmpty() && t >= unAvailable.peek()[0]) {
20+
val (timeFree, weight, index) = unAvailable.poll()
21+
available.add(intArrayOf(weight, index))
22+
}
23+
24+
val (weight, index) = available.poll()
25+
res[i] = index
26+
unAvailable.add(intArrayOf(t + tasks[i], weight, index))
27+
}
28+
29+
return res
30+
}
31+
}

0 commit comments

Comments
 (0)