Greedy Algorithms8 sections · 316 units
Open in Course

Task Scheduler - Implementation

The code

function leastInterval(tasks, n)
 count := array of 26 zeros
 for each task in tasks
 count[task - 'A'] := count[task - 'A'] + 1
 maxFreq := max of count
 maxCount := number of elements in count equal to maxFreq
 return max(length of tasks, (maxFreq - 1) * (n + 1) + maxCount)

Time: O(n)O(n). Space: O(1)O(1) (2626 letters).