Created
November 8, 2018 22:33
-
-
Save hsaputra/668ceb02684ec052231b7030b5947412 to your computer and use it in GitHub Desktop.
621. Task Scheduler - https://leetcode.com/problems/task-scheduler/
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
class Solution { | |
public int leastInterval(char[] tasks, int n) { | |
if (n < 0 ) { | |
return 0; | |
} | |
// Group tasks | |
int[] count = new int[26]; | |
for (char cur : tasks) { | |
int pos = cur - 'A'; | |
count[pos]++; | |
} | |
// Sort | |
Arrays.sort(count); | |
// Iterate through sorted array and for each larger elements go to diff tasks | |
// until n cool down | |
int time = 0; | |
while(count[25] > 0) { | |
int i = 0; | |
while (i <= n) { | |
// stopping | |
if (count[25] == 0) { | |
break; | |
} | |
if (i < 26 && count[25 - i] > 0) { | |
count[25 - i]--; | |
} | |
time++; | |
i++; | |
} | |
// Resort | |
Arrays.sort(count); | |
} | |
return time; | |
} | |
} |
Author
hsaputra
commented
Nov 9, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment