LeetCode 621 Task Scheduler - Implementation

The approach

Calculate based on max frequency and count of max-frequency tasks.

function leastInterval(tasks, n): count = [0] * 26 for task in tasks: count[ord(task) - ord('A')] += 1 maxCount = max(count) numMaxTasks = count.count(maxCount) result = (maxCount - 1) * (n + 1) + numMaxTasks return max(len(tasks), result)

O(n)O(n) time, O(1)O(1) space.