forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeastInterval.java
More file actions
24 lines (21 loc) · 737 Bytes
/
LeastInterval.java
File metadata and controls
24 lines (21 loc) · 737 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
package dynamic;
import java.util.Arrays;
public class LeastInterval {
public int leastInterval(char[] tasks,int n){
int[] map = new int[26];
for (char c : tasks) map[c-'A']++;
Arrays.sort(map);
int max_val = map[25] -1,idle_slots = max_val*n;
for (int i = 24;i>=0 && map[i] > 0;i--){
idle_slots -= Math.min(map[i],max_val);
}
return idle_slots >0 ? idle_slots+tasks.length : tasks.length;
}
public static void main(String[] args) {
char[] tasks = {'A','A','A','B','B','B'};
int n = 2;
LeastInterval leastInterval = new LeastInterval();
int r = leastInterval.leastInterval(tasks,n);
System.out.println(r);
}
}