-
Notifications
You must be signed in to change notification settings - Fork 5
/
Day-28 Task Schedular
49 lines (38 loc) · 1.07 KB
/
Day-28 Task Schedular
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
public int leastInterval(char[] tasks, int n) {
// frequencies of the tasks
int[] frequencies = new int[26];
for (int t : tasks) {
frequencies[t - 'A']++;
}
Arrays.sort(frequencies);
// max frequency
int f_max = frequencies[25];
int idle_time = (f_max - 1) * n;
for (int i = frequencies.length - 2; i >= 0 && idle_time > 0; --i) {
idle_time -= Math.min(f_max - 1, frequencies[i]);
}
idle_time = Math.max(0, idle_time);
return idle_time + tasks.length;
}
}
class Solution {
public int leastInterval(char[] tasks, int n) {
int[] count = new int[26];
for(char c : tasks){
count[c - 'A']++;
}
int maxFreq = 0;
int groupSize = 0;
for(int i : count){
if(i > maxFreq){
maxFreq = i;
groupSize = 0;
}
if(maxFreq == i){
groupSize++;
}
}
return Math.max(tasks.length, (n + 1) * (maxFreq - 1) + groupSize);
}
}