-
Notifications
You must be signed in to change notification settings - Fork 21k
Expand file tree
/
Copy pathTaskScheduler.java
More file actions
38 lines (29 loc) · 1.06 KB
/
TaskScheduler.java
File metadata and controls
38 lines (29 loc) · 1.06 KB
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
import java.util.*;
public class TaskScheduler {
public static int leastInterval(char[] tasks, int n) {
int[] freq = new int[26];
for (char c : tasks) {
freq[c - 'A']++;
}
Arrays.sort(freq);
int maxFreq = freq[25];
int idleSlots = (maxFreq - 1) * n;
// Fill idle slots with remaining tasks
for (int i = 24; i >= 0 && idleSlots > 0; i--) {
idleSlots -= Math.min(freq[i], maxFreq - 1);
}
idleSlots = Math.max(0, idleSlots);
return tasks.length + idleSlots;
}
public static void main(String[] args) {
char[] tasks1 = {'A','A','A','B','B','B'};
int n1 = 2;
System.out.println("Output 1: " + leastInterval(tasks1, n1)); // 8
char[] tasks2 = {'A','C','A','B','D','B'};
int n2 = 1;
System.out.println("Output 2: " + leastInterval(tasks2, n2)); // 6
char[] tasks3 = {'A','A','A','B','B','B'};
int n3 = 0;
System.out.println("Output 3: " + leastInterval(tasks3, n3)); // 6
}
}