Created
April 4, 2021 08:05
-
-
Save ajaynitt/3e879630e219d468fc935da926c0f6e6 to your computer and use it in GitHub Desktop.
This file contains 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
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