InterviewHubPRO
Section Overview
Q69

Question 69 of 100

Part 8: Heaps, Priority Queues & Tries (65 - 72)

69%
DSA Patterns (Multi-Lang)•Part 8: Heaps, Priority Queues & Tries (65 - 72)

Task Scheduler.

In Simple Words: Mathematical formula based on maximum task frequency maxFreq: (maxFreq - 1) * (n + 1) + countOfMaxFreqTasks. Answer is max(len(tasks), formulaResult).

  • Time: $O(N)$ | Space: $O(26) = O(1)$
Multi-Language Solutions
Switch tabs or choose All
from collections import Counter

def leastInterval(tasks: list[str], n: int) -> int:
    counts = Counter(tasks)
    max_f = max(counts.values())
    max_count = sum(1 for c in counts.values() if c == max_f)
    return max(len(tasks), (max_f - 1) * (n + 1) + max_count)