-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.java
28 lines (28 loc) · 881 Bytes
/
Solution.java
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
class Solution {
public int leastInterval(char[] tasks, int n) {
PriorityQueue<Integer> q = new PriorityQueue<>(26, Collections.reverseOrder());
int map[] = new int[26];
for (char c : tasks) {
map[c - 'A']++;
}
Arrays.stream(map).filter(x -> x != 0).forEach(q::offer);
int time = 0;
while (!q.isEmpty()) {
List<Integer> temp = new LinkedList<>();
for (int i = 0; i <= n; i++) {
if (!q.isEmpty()) {
int cur = q.poll() - 1;
if (cur > 0) {
temp.add(cur);
}
}
time++;
if (q.isEmpty() && temp.size() == 0) {
break;
}
}
temp.forEach(q::offer);
}
return time;
}
}