class Solution {
fun scheduleCourse(courses: Array<IntArray>): Int {
courses.sortBy { it[1] } // ascending order by last day
val queue = PriorityQueue<Int>(compareBy { -it }) // descending order by duration
var curr = 0
courses.forEach { course ->
curr += course[0] // accumulate duration
queue.add(course[0])
if (curr > course[1]) { // exceed last day
curr -= queue.poll() // remove biggest duration course
}
}
return queue.size
}
}