Greedy Algorithms8 sections · 316 units
Open in Course

Task Scheduler - Algorithm

Count and formula

Let ff be the maximum frequency and kk be the number of tasks with that frequency. The minimum time is: max(total tasks,(f1)×(n+1)+k)\max(\text{total tasks}, (f-1) \times (n+1) + k). Why? The most frequent task creates (f1)(f-1) gaps of size (n+1)(n+1), plus the final batch of kk tasks.

If other tasks fill the gaps, the formula gives total tasks. Otherwise, idle time is needed. Time: O(n)O(n). Space: O(1)O(1).