forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#1679 from a93a/1834
Create 1834-single-threaded-cpu.kt
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |