Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1658 from a93a/621
Browse files Browse the repository at this point in the history
Create 621-Task-Scheduler.kt
  • Loading branch information
Ahmad-A0 authored Dec 29, 2022
2 parents 6f71dc7 + e2e9107 commit 77f0bb2
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions kotlin/621-Task-Scheduler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
fun leastInterval(tasks: CharArray, n: Int): Int {
if(n == 0)
return tasks.size
val hm = HashMap<Char,Int>()
for(task in tasks)
hm[task] = hm.getOrDefault(task, 0) + 1
val maxHeap = PriorityQueue<Int>(compareBy{-it})
for(count in hm.values)
maxHeap.add(count)
val q = ArrayDeque<Pair<Int,Int>>() // time, value
var time = 0
while(!maxHeap.isEmpty() || !q.isEmpty()){
if(!maxHeap.isEmpty()){
var current = maxHeap.poll()
current--
if(current > 0)
q.add(Pair(time+n,current))
}
if(!q.isEmpty()){
if(q.peek().first == time)
maxHeap.add(q.poll().second)
}
time++
}
return time
}
}

0 comments on commit 77f0bb2

Please sign in to comment.