Task Scheduler
Description
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.Solutions
Last updated
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.Last updated
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
if(tasks.empty()) return 0;
vector<int> freqs(26);
for(auto task : tasks)
++freqs[task - 'A'];
sort(freqs.begin(), freqs.end(), greater<int>());
int f = freqs[0], i = 1;
while(i < 26 && freqs[i] == f) ++i;
int frame_size = max(i, n + 1) * (f - 1) + i;
int num = i * f;
while(i < 26 && freqs[i] > 0){
num += freqs[i];
++i;
}
return max(num, frame_size);
}
};class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
if(tasks.empty()) return 0;
vector<int> freqs(26);
for(auto task : tasks)
++freqs[task - 'A'];
sort(freqs.begin(), freqs.end(), greater<int>());
int f = freqs[0], i = 1;
while(i < 26 && freqs[i] == f) ++i;
int frame_size = max(i, n + 1) * (f - 1) + i;
int num = tasks.size();
return max(num, frame_size);
}
};