Created
November 2, 2021 02:52
-
-
Save tolinwei/511bf3c85ecf36b17ac563240885002e to your computer and use it in GitHub Desktop.
Task Scheduler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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