MediumPro challengePythonJavaScriptTypeScript
Greedy — Task Scheduler
AlgorithmsGreedyMath
solve(tasks, n) — tasks is an array of single-letter task names. Each task takes
one time unit, and the CPU may also sit idle. Two executions of the same task must
be separated by at least n units. Return the minimum number of units needed to
finish all tasks.
You do not need to simulate. The most frequent task forces a skeleton of(maxFreq - 1) blocks of length n + 1, followed by one final row containing every
task that has the maximum frequency. If there are so many different tasks that no
idling is required, the answer is simply the number of tasks.
Examples
solve(["A","A","A","B","B","B"], 2) → 8
solve(["A","A","A","B","B","B"], 0) → 6
solve(["A","A","A","A","A","A","B","C","D","E","F","G"], 2) → 16
solve(["A"], 5) → 1Sample tests
Test #1Classic example
Input: [["A","A","A","B","B","B"],2]
Output: 8
Test #2No cooldown
Input: [["A","A","A","B","B","B"],0]
Output: 6
Test #3One dominant task
Input: [["A","A","A","A","A","A","B","C","D","E","F","G"],2]
Output: 16