-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_43.java
More file actions
33 lines (33 loc) · 972 Bytes
/
Copy pathDay_43.java
File metadata and controls
33 lines (33 loc) · 972 Bytes
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
// Task Scheduler
class Solution {
public int leastInterval(char[] tasks, int n) {
int size = tasks.length;
int[] freq = new int[26];
for(int i=0;i<size;i++){
freq[tasks[i] - 'A']++;
}
PriorityQueue<Integer> pq = new PriorityQueue<>((a,b) -> Integer.compare(b,a));
for(int i=0;i<26;i++){
if(freq[i] != 0){
pq.offer(freq[i]);
}
}
int cnt = 0;
while(!pq.isEmpty()){
int tcycle = 0;
int cycle = n+1;
List<Integer> list = new ArrayList<>();
while(cycle > 0 && !pq.isEmpty()){
int currtask = pq.poll();
if(currtask>1) list.add(currtask - 1);
tcycle++;
cycle--;
}
for(int it : list){
pq.offer(it);
}
cnt += pq.isEmpty() ? tcycle : n+1;
}
return cnt;
}
}