forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1834-single-threaded-cpu.kt
31 lines (31 loc) · 1018 Bytes
/
1834-single-threaded-cpu.kt
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 getOrder(tasks: Array<IntArray>): IntArray {
val res = mutableListOf<Int>()
val sorted = Array(tasks.size){ IntArray(3) }
for(i in tasks.indices){
sorted[i][0] = tasks[i][0]
sorted[i][1] = tasks[i][1]
sorted[i][2] = i
}
sorted.sortBy{it -> it[0]}
val pq: PriorityQueue<IntArray> = PriorityQueue{a, b ->
if(a[0] == b[0]) a[1] - b[1]
else a[0] - b[0]
}
var time = sorted[0][0]; var index = 0
while(!pq.isEmpty() || index < sorted.size){
while(index < sorted.size && time >= sorted[index][0]){
pq.offer(intArrayOf(sorted[index][1],sorted[index][2]))
index++
}
if(!pq.isEmpty()){
val (pT, i) = pq.poll()
time += pT
res.add(i)
}else{
time = sorted[index][0]
}
}
return res.toIntArray()
}
}