Schedule tasks with a cooldown so identical tasks are separated by at least n intervals, and compute the minimum total time needed.
You are given an array of tasks, where each task is represented by a capital letter, and an integer cooldown period n.
In one unit of time, you may either execute one task or stay idle. The same task type cannot be executed again until at least n other time units have passed since its last execution.
Return the minimum number of time units required to finish all tasks.
The goal is to arrange the task executions so that the cooldown constraint is satisfied while minimizing idle time.
tasks: an array of task labels, typically uppercase letters.n: a non-negative integer cooldown interval.Return a single integer: the minimum number of time units needed to complete all tasks.
1 <= tasks.lengthtasks[i] is a task label.0 <= nExample 1
Input
tasks = ["A","A","A","B","B","B"], n = 2
Output
8
Explanation
One optimal schedule is A -> B -> idle -> A -> B -> idle -> A -> B, which takes 8 time units.
Example 2
Input
tasks = ["A","A","A","B","B","B"], n = 0
Output
6
Explanation
No cooldown is required, so the tasks can be executed back to back in any order.
Example 3
Input
tasks = ["A","A","A","B","B","B"], n = 1
Output
6
Explanation
One valid schedule is A -> B -> A -> B -> A -> B, which uses no idle time.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.