-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathleastInterval.java
47 lines (36 loc) · 1.16 KB
/
leastInterval.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution {
public int leastInterval(char[] tasks, int n) {
int[] map = new int[26];
for(char task : tasks)
map[task - 'A']++;
PriorityQueue<Integer> queue = new PriorityQueue<>(26, Collections.reverseOrder());
for(int entry : map)
if (entry > 0)
queue.offer(entry);
int time = 0;
while(!queue.isEmpty())
{
int i = 0;
List<Integer> temp = new ArrayList<>();
while(i <= n)
{
if(!queue.isEmpty())
{
if(queue.peek()>1)
{
temp.add(queue.poll()-1);
}
else
queue.poll();
}
time++;
if(queue.isEmpty() && temp.size()==0)
break;
i++;
}
for(int a : temp)
queue.offer(a);
}
return time;
}
}