Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.

However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.

You need to return the least number of intervals 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.
```

Note:

1. The number of tasks is in the range [1, 10000].

CPU需要处理一系列任务，限制条件是两个相同的任务被处理的时间至少需要间隔n时刻，问CPU最少需要多长时间能处理完所有任务。

```class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
map<char, int> count;
for (const auto& c : tasks)++count;
priority_queue<pair<int, char>> pq;
for (const auto& p : count)pq.push({ p.second,p.first });
int cycle = n + 1, time = 0;
while (!pq.empty()) {
vector<pair<int, char>> tmp;
int tsk = 0;
for (int i = 0; i < cycle; ++i) {
if (!pq.empty()) {
tmp.push_back(pq.top());
pq.pop();
++tsk;
}
}
for (auto& t : tmp) {
if (--t.first) {
pq.push(t);
}
}
time += pq.empty() ? tsk : cycle;
}
return time;
}
};
```