Given a list of tasks, each represented by a letter (from A to Z), and a cooldown period k
. The cooldown period specifies that the same task must be separated by at least k
intervals between executions.
Determine the minimum number of intervals required to complete all tasks, while ensuring that the same task is not executed again before the cooldown period has passed.
tasks: string[]
: An array of characters, where each task is represented as a letter (A-Z)k: number
: An integer representing the cooldown period, the minimum number of intervals between two executions of the same taskInput: tasks = ["A","A","A","B","B","B"], n = 2Output: 8Explanation: Task 'A' appears 3 times and task 'B' appears 3 times. Since there is a cooldown of 2, we need to ensure that after every 'A' there are at least 2 other tasks or idle intervals before we can add another 'A'. The same applies to 'B'. A valid sequence might look like this: 'A' -> 'B' -> idle -> 'A' -> 'B' -> idle -> 'A' -> 'B'. Thus, the total number of intervals required is 8.
Input: tasks = ["A","C","A","B","D","B"], n = 1Output: 6Explanation: The tasks 'A', 'B', and 'C' appear multiple times, but the cooldown is only 1. After completing one 'A', we can immediately complete another 'A' after just one other task. A valid sequence could look like this: 'A' -> 'C' -> 'A' -> 'B' -> 'D' -> 'B'. Thus, the total number of intervals required is 6.
Input: tasks = ["A","A","A","B","B","B"], n = 3Output: 10Explanation: The cooldown is 3, meaning after completing a task, we must wait for 3 intervals before repeating the same task. A valid sequence could be: 'A' -> 'B' -> idle -> idle -> 'A' -> 'B' -> idle -> idle -> 'A' -> 'B'. Thus, the total number of intervals needed is 10.
tasks.length
<= 1000tasks[i]
is an uppercase English letterk
<= 100Given a list of tasks, each represented by a letter (from A to Z), and a cooldown period k
. The cooldown period specifies that the same task must be separated by at least k
intervals between executions.
Determine the minimum number of intervals required to complete all tasks, while ensuring that the same task is not executed again before the cooldown period has passed.
tasks: string[]
: An array of characters, where each task is represented as a letter (A-Z)k: number
: An integer representing the cooldown period, the minimum number of intervals between two executions of the same taskInput: tasks = ["A","A","A","B","B","B"], n = 2Output: 8Explanation: Task 'A' appears 3 times and task 'B' appears 3 times. Since there is a cooldown of 2, we need to ensure that after every 'A' there are at least 2 other tasks or idle intervals before we can add another 'A'. The same applies to 'B'. A valid sequence might look like this: 'A' -> 'B' -> idle -> 'A' -> 'B' -> idle -> 'A' -> 'B'. Thus, the total number of intervals required is 8.
Input: tasks = ["A","C","A","B","D","B"], n = 1Output: 6Explanation: The tasks 'A', 'B', and 'C' appear multiple times, but the cooldown is only 1. After completing one 'A', we can immediately complete another 'A' after just one other task. A valid sequence could look like this: 'A' -> 'C' -> 'A' -> 'B' -> 'D' -> 'B'. Thus, the total number of intervals required is 6.
Input: tasks = ["A","A","A","B","B","B"], n = 3Output: 10Explanation: The cooldown is 3, meaning after completing a task, we must wait for 3 intervals before repeating the same task. A valid sequence could be: 'A' -> 'B' -> idle -> idle -> 'A' -> 'B' -> idle -> idle -> 'A' -> 'B'. Thus, the total number of intervals needed is 10.
tasks.length
<= 1000tasks[i]
is an uppercase English letterk
<= 100console.log()
statements will appear here.