Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1679 from a93a/1834
Browse files Browse the repository at this point in the history
Create 1834-single-threaded-cpu.kt
  • Loading branch information
Ahmad-A0 authored Dec 29, 2022
2 parents 1d4e7e2 + 9d9ddf3 commit 1086c5c
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions kotlin/1834-single-threaded-cpu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
}
}

0 comments on commit 1086c5c

Please sign in to comment.