Description

Given a characters array tasks representing tasks a CPU needs to do, and a non-negative integer n representing the cooldown period between two same tasks. Return the least number of units of time that the CPU will take to finish all tasks.

Examples

Input:tasks = ["A","A","A","B","B","B"], n = 2
Output:8
Explanation:

A -> B -> idle -> A -> B -> idle -> A -> B

Input:tasks = ["A","A","A","B","B","B"], n = 0
Output:6
Explanation:

No cooldown needed, all 6 tasks can be done consecutively.

Input:tasks = ["A","A","B","C","D"], n = 3
Output:7
Explanation:

With cooldown n=3, the requirement is 3 units between same tasks. Optimal schedule: A -> B -> C -> D -> A. The second A can be scheduled after position 4 since there are 3 tasks (B,C,D) between the two A's, satisfying the cooldown requirement. Total time: 5 tasks with no idle time needed.

Constraints

  • 1 ≤ tasks.length ≤ 10⁴
  • tasks[i] is an uppercase English letter.
  • 0 ≤ n ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!