Task Scheduler
1 approach, code in every language
Problem description
You are given an array of CPU tasks, each labelled with an uppercase letter, and an integer n that represents the cooldown period. Two identical tasks must be separated by at least n time units, during which the CPU may either run a different task or stay idle.
Every task takes exactly one time unit. Return the minimum number of time units the CPU needs to finish all of the tasks, inserting idle slots wherever they are unavoidable.
Example 1
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
One valid ordering is A B idle A B idle A B. The three A's are each two units apart, and so are the three B's, giving 8 total units.
Example 2
Input: tasks = ["A","C","A","B","D","B"], n = 1
Output: 6
There are enough distinct tasks to keep the CPU busy, so no idle slots are needed and the answer equals the number of tasks.
Constraints
1 <= tasks.length <= 10^4tasks[i] is an uppercase English letter.0 <= n <= 100
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Press Die Cooldown is our own problem on the technique behind Task Scheduler: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free