2018-06-28 14:09:05         来源：Pilgrim

```
AAAABBBEEFFGG 3

A---A---A---A

AB--AB--AB--A(加入B)

ABE-ABE-AB--A(加入E)

ABEFABE-ABF-A(加入F，每次尽可能填满或者是均匀填充)

ABEFABEGABFGA(加入G)

ACCCEEE 2

CE-CE-CE

CEACE-CE(加入A)

```bool cmp(const int a, const int b) {
return a > b;
}
class Solution {
public:
int leastInterval(vector& tasks, int n) {
int counter[26];
memset(counter, 0, sizeof(counter));
for (int i = 0; i < tasks.size(); i++) {
counter[ tasks[i] - 'A' ] ++ ;
}
sort(counter, counter+26, cmp);
int ret = 0, done = 0;
int mx = counter[0];
int i = 0;
while (i < 26 && counter[i] == mx) i ++;
return max((int)tasks.size(), (mx-1) * (n+1) +  i);
// while ( done < tasks.size() ) {
//  int cur = 0;
//  bool first = true;
//  for (int i = 0; i < 26; i++) {
//if (counter[i] != 0) {
// if (first) pos = i, first = false;
// cur ++;
// counter[i] --;
// done++;
//}
//if (cur >= n+1) break;
//  }
//  if (cur < n+1 && done < tasks.size() && ( last == pos|| last == -1)) ret += n + 1 - cur;
//  ret += cur;
//  last = pos;
// }
// return ret;
}
};```