Skip to content

Instantly share code, notes, and snippets.

@ajaynitt
Created April 4, 2021 08:05
Show Gist options
  • Save ajaynitt/3e879630e219d468fc935da926c0f6e6 to your computer and use it in GitHub Desktop.
Save ajaynitt/3e879630e219d468fc935da926c0f6e6 to your computer and use it in GitHub Desktop.
621. Task Scheduler
https://leetcode.com/submissions/detail/476271316/
Solution :
public static int leastInterval2(char[] tasks, int n) {
Integer[] taskOccurence = new Integer[26];
Arrays.fill(taskOccurence, 0);
for (char task : tasks) {
taskOccurence[task - 'A']++;
}
Collections.sort(Arrays.asList(taskOccurence));
int adjustedVal = 0,lastIndex=taskOccurence.length -1;
while (lastIndex > 0 && Objects.equals(taskOccurence[lastIndex], taskOccurence[lastIndex-1])) {
adjustedVal++;
lastIndex--;
}
int vacanciesCreatedByLargestOccuringNum = (taskOccurence[25] - 1) * n;
int elementsToBefilledInVacancy = 0;
for (int i =0 ;i < 25;i++){
elementsToBefilledInVacancy += taskOccurence[i];
}
//adjusting the elemnts occurence which are same as of last elemnts
elementsToBefilledInVacancy -= adjustedVal;
int elementsbeyondVacancy = Math.max(elementsToBefilledInVacancy-vacanciesCreatedByLargestOccuringNum,0);
return taskOccurence[25] + vacanciesCreatedByLargestOccuringNum + elementsbeyondVacancy + adjustedVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment