做法很多,这位同学的解释是最通俗易懂的:【任务调度器】C++ 桶子_配图理解
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
int len = tasks.size();
vector<int> vec(26); //26个字母
for(char c : tasks) ++vec[c - 'A'];
sort(vec.begin(),vec.end(),[](int& x,int&y){return x>y;});
int i = 1;
while(i < vec.size() && vec[i] == vec[0]) ++i;
return max(len, i + (n + 1) * (vec[0]-1) );
}
};