## Algorithm

Given a characters array `tasks`, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.

However, there is a non-negative integer `n` that represents the cooldown period between two same tasks (the same letter in the array), that is that there must be at least `n` units of time between any two same tasks.

Return the least number of units of times that the CPU will take to finish all the given tasks.

Example 1:

```Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation:
A -> B -> idle -> A -> B -> idle -> A -> B
There is at least 2 units of time between any two same tasks.
```

Example 2:

```Input: tasks = ["A","A","A","B","B","B"], n = 0
Output: 6
Explanation: On this case any permutation of size 6 would work since n = 0.
["A","A","A","B","B","B"]
["A","B","A","B","A","B"]
["B","B","B","A","A","A"]
...
And so on.
```

Example 3:

```Input: tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
Output: 16
Explanation:
One possible solution is
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A
```

Constraints:

• `1 <= task.length <= 104`
• `tasks[i]` is upper-case English letter.
• The integer `n` is in the range `[0, 100]`.

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
int cmp(const void *a, const void *b) {
return *(int *)a < *(int *)b ?  1 :
*(int *)a > *(int *)b ? -1 : 0;
}
int c = { 0 };
int i, k;
for (i = 0; i < tasksSize; i ++) {
}
qsort(c, 26, sizeof(int), cmp);
i = 1;
while (i < 26 && c[i] == c) {
i ++;
}
k = (c - 1) * (n + 1) + i;
}
``````
Copy The Code &

Input

cmd
tasks = ["A","A","A","B","B","B"], n = 2

Output

cmd
8

### #2 Code Example with C++ Programming

```Code - C++ Programming```

``````
class Solution {
public:
int leastInterval(vector& tasks, int n) {
unordered_mapm;
priority_queue>pq;
for(auto x: m) pq.push(make_pair(x.second, x.first));
int sum = 0;
while(!pq.empty()){
int time = 0;
vector>v;
for(int i = 0; i < n + 1; i++)
if(!pq.empty()){
v.push_back(pq.top());
pq.pop();
time++;
}
for(auto x: v) if(--x.first) pq.push(x);
sum += !pq.empty() ? n + 1 : time;
}
return sum;
}
};
``````
Copy The Code &

Input

cmd
tasks = ["A","A","A","B","B","B"], n = 2

Output

cmd
8

### #3 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public int leastInterval(char[] tasks, int n) {
int[] frequencies = new int;
}
Arrays.sort(frequencies);
int maxFrequency = frequencies;
int idleTime = (maxFrequency - 1) * n;
for (int i = frequencies.length - 2; i >= 0 && idleTime > 0; i--) {
idleTime -= Math.min(maxFrequency - 1, frequencies[i]);
}
idleTime = Math.max(0, idleTime);
}
}
``````
Copy The Code &

Input

cmd
tasks = ["A","A","A","B","B","B"], n = 0

Output

cmd
6

### #4 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const leastInterval = function (tasks, n) {
const cnt = Array(26).fill(0)

const A = 'A'.charCodeAt(0)
let maxFreq = 0,
maxFreqCnt = 0
for (const ch of tasks) {
const idx = ch.charCodeAt(0) - A
cnt[idx]++
if (maxFreq === cnt[idx]) {
maxFreqCnt++
} else if (maxFreq < cnt[idx]) {
maxFreqCnt = 1
maxFreq = cnt[idx]
}
}

const slot = maxFreq - 1
const numOfPerSlot = n - (maxFreqCnt - 1)
const available = len - maxFreq * maxFreqCnt
const idles = Math.max(0, slot * numOfPerSlot - available)
return len + idles
}
``````
Copy The Code &

Input

cmd
tasks = ["A","A","A","B","B","B"], n = 0

Output

cmd
6

### #5 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
idles = (cnt[-1] - 1) * n
for i in range(len(cnt) - 1): idles -= min(cnt[i], cnt[-1] - 1)
``````
Copy The Code &

Input

cmd
tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2

Output

cmd
16

### #6 Code Example with C# Programming

```Code - C# Programming```

``````
using System;

namespace LeetCode
{
{
public int LeastInterval(char[] tasks, int n)
{
var count = new int;
count[ch - 'A']++;

Array.Sort(count);
var max = count;
var left_count = max - 1;
var idleTime = left_count * (n + 1);
for (int i = 25; i >= 0 && count[i] > 0 && idleTime > 0; i--)
idleTime -= Math.Min(left_count, count[i]);

if (idleTime > 0) return idleTime + tasks.Length;
}
}
}
``````
Copy The Code &

Input

cmd
tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2

Output

cmd
16