Skip to content

Instantly share code, notes, and snippets.

@tolinwei
Created November 2, 2021 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolinwei/511bf3c85ecf36b17ac563240885002e to your computer and use it in GitHub Desktop.
Save tolinwei/511bf3c85ecf36b17ac563240885002e to your computer and use it in GitHub Desktop.
Task Scheduler
public int leastInterval(char[] tasks, int n) {
int[] counter = new int[26];
int max = 0; // 最多的字母的频数
int maxCount = 0; // 有几个这样子的字母
for(char task : tasks) {
counter[task - 'A']++;
if(max == counter[task - 'A']) {
maxCount++;
}
else if(max < counter[task - 'A']) {
max = counter[task - 'A'];
maxCount = 1;
}
}
int segmentCount = max - 1;
int segmentEmptySlots = n + 1 - maxCount;
int emptySlots = segmentCount * segmentEmptySlots;
int remainingTasks = tasks.length - max * maxCount;
// 最难想的是最后两句
int idles = Math.max(0, emptySlots - remainingTasks);
return tasks.length + idles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment