Task Scheduler
Given a characters array `tasks`, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle. However, there is a non-negative integer `n` that represents the cooldown period between two same tasks. Return the least number of units of times that the CPU will take to finish all the given tasks. **Examples:** ```typescript leastInterval(["A","A","A","B","B","B"], 2) // 8 (A -> B -> idle -> A -> B -> idle -> A -> B) leastInterval(["A","A","A","B","B","B"], 0) // 6 ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(tasks.length)
- Space complexity: O(1)
Single linear pass counts frequencies of 26 uppercase task letters.
Follow-Up Questions Interviewers Ask About Task Scheduler
- What is the formula-based intuition behind Task Scheduler?
The most frequent task creates (maxFreq - 1) frames separated by cooldown slots n. Other tasks fill those idle slots.
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.