-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskScheduler.java
More file actions
43 lines (34 loc) · 1.04 KB
/
Copy pathTaskScheduler.java
File metadata and controls
43 lines (34 loc) · 1.04 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
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
class Solution {
public int leastInterval(char[] tasks, int n) {
Map<Character, Integer> map = new HashMap<>();
for (char c : tasks) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
Queue<Integer> pQueue = new PriorityQueue<>(Comparator.reverseOrder());
pQueue.addAll(map.values());
int time = 0;
List<Integer> temp;
while (!pQueue.isEmpty()) {
temp = new ArrayList<>();
for (int i = 0; i < n+1; i++) {
if (!pQueue.isEmpty()) {
temp.add(pQueue.poll());
}
}
for (int count : temp) {
if (--count > 0) {
pQueue.add(count);
}
}
time += pQueue.isEmpty() ? temp.size() : n+1;
}
return time;
}
}