forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1882-process-tasks-using-servers.kt
More file actions
31 lines (24 loc) · 979 Bytes
/
1882-process-tasks-using-servers.kt
File metadata and controls
31 lines (24 loc) · 979 Bytes
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
class Solution {
fun assignTasks(servers: IntArray, tasks: IntArray): IntArray {
val res = IntArray (tasks.size)
val available = PriorityQueue<IntArray>(compareBy ({ it[0] }, { it[1] })).apply {
for ((index, weight) in servers.withIndex())
this.add(intArrayOf(weight, index))
}
val unAvailable = PriorityQueue<IntArray>(compareBy { it[0] })
var t = 0
for (i in tasks.indices) {
t = maxOf(t, i)
if (available.isEmpty())
t = unAvailable.peek()[0]
while (unAvailable.isNotEmpty() && t >= unAvailable.peek()[0]) {
val (timeFree, weight, index) = unAvailable.poll()
available.add(intArrayOf(weight, index))
}
val (weight, index) = available.poll()
res[i] = index
unAvailable.add(intArrayOf(t + tasks[i], weight, index))
}
return res
}
}