-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskScheduler.java
More file actions
46 lines (37 loc) · 1.33 KB
/
TaskScheduler.java
File metadata and controls
46 lines (37 loc) · 1.33 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
39
40
41
42
43
44
45
46
import java.util.*;
class TaskScheduler {
public static void main(String[] args) {
char[] test = {'A','A','A','B','B','B'};
int n = 2;
System.out.println(leastInterval(test,n));
}
public static int leastInterval(char[] tasks, int n) {
if(n == 0) return tasks.length;
Map<Character,Integer> map = new HashMap<>();
for(char c: tasks) {
map.put(c, map.getOrDefault(c,0)+1);
}
PriorityQueue<Map.Entry<Character,Integer>> maxHeap = new PriorityQueue<>((i1, i2) -> i2.getValue() - i1.getValue());
maxHeap.addAll(map.entrySet());
int count = 0;
while(!maxHeap.isEmpty()) {
int interval = n + 1;
List<Map.Entry<Character, Integer>> list = new ArrayList<>();
while(interval > 0 && !maxHeap.isEmpty()) {
Map.Entry<Character,Integer> entry = maxHeap.poll();
entry.setValue(entry.getValue()-1);
list.add(entry);
interval--;
count++;
}
for(Map.Entry<Character,Integer> entry: list) {
if(entry.getValue() > 0) {
maxHeap.offer(entry);
}
}
if(maxHeap.isEmpty()) break;
count+= interval;
}
return count;
}
}