Description
Submission
class Solution {
typedef pair<int, int> PII;
int cnt[26];
public:
int leastInterval(vector<char>& tasks, int n) {
priority_queue<PII, vector<PII>> pq;
for(auto ch: tasks) {
++cnt[ch-'A'];
}
for(int i = 0; i < 26; ++i) {
if(cnt[i]) pq.push({cnt[i], i});
}
int ret = 0;
while(!pq.empty()) {
vector<int> tmp(26, 0);
int pqSize = pq.size();
for(int i = 0; !pq.empty() && i < n + 1; ++i) {
auto [count, index] = pq.top();
pq.pop();
count -= 1;
if(count) tmp[index] = count;
}
for(int i = 0; i < 26; ++i) {
if(tmp[i]) pq.push({tmp[i], i});
}
if(pq.empty()) ret += pqSize;
else ret += n + 1;
}
return ret;
}
};