You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
972 B

2 years ago
package class38;
public class Problem_0621_TaskScheduler {
// ['A', 'B', 'A']
public static int leastInterval(char[] tasks, int free) {
int[] count = new int[256];
// 出现最多次的任务,到底是出现了几次
int maxCount = 0;
for (char task : tasks) {
count[task]++;
maxCount = Math.max(maxCount, count[task]);
}
// 有多少种任务,都出现最多次
int maxKinds = 0;
for (int task = 0; task < 256; task++) {
if (count[task] == maxCount) {
maxKinds++;
}
}
// maxKinds : 有多少种任务,都出现最多次
// maxCount : 最多次,是几次?
// 砍掉最后一组剩余的任务数
int tasksExceptFinalTeam = tasks.length - maxKinds;
int spaces = (free + 1) * (maxCount - 1);
// 到底几个空格最终会留下!
int restSpaces = Math.max(0, spaces - tasksExceptFinalTeam);
return tasks.length + restSpaces;
// return Math.max(tasks.length, ((n + 1) * (maxCount - 1) + maxKinds));
}
}