int LeastInterval(char* tasks, int tasksSize, int n)
{
int hash[26] = {0};
int result;
int i;
for (i = 0; i < tasksSize; i++) {
hash[tasks[i] - 'A']++;
}
// 找出最大次数的任务
int j;
int max = hash[0];
for (j = 1; j < 26; j++) {
if (hash[j] > max) {
max = hash[j];
}
}
// 找出来最大次数相同的任务到底有几个,以及和间隔n的关系
int maxNum = 0;
for (j = 0; j < 26; j++) {
if (hash[j] == max) {
maxNum++;
}
}
// 求和其他任务的总和
int sumOther;
sumOther = tasksSize - max;
// 分情况讨论
if ((sumOther <= (max - 1)*n) && maxNum == 1) { // 如果其他任务数小于最大任务数的间隔,且最大任务数唯一
result = (max - 1)*n + max;
} else if ((sumOther <= (max - 1)*n) && maxNum > 1) { // 如果其他任务数小于最大任务数的间隔,且最大任务数不唯一
result = (max - 1) * n + max + (maxNum - 1);
} else {
result = sumOther + max; // 如果其他任务数大于最大任务数的间隔,那么必然可以完美调度
}
return result;
}