BiruLyu
7/3/2017 - 3:50 AM

``````public class Solution {
public int leastInterval(char[] tasks, int n) {
if (tasks == null) return 0;
Map<Character, Integer> map = new HashMap<>();
int maxFrequency = 0;
for (char c : tasks) {
map.put(c, map.getOrDefault(c, 0) + 1);
maxFrequency = Math.max(maxFrequency, map.get(c));
}
int count = 0;
for (int frequency : map.values()) {
if (frequency == maxFrequency) {
count++;
}
}
return Math.max(len, (maxFrequency - 1) * (n + 1)  + count);
}
}``````
``````public 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);
}
PriorityQueue<Integer> queue = new PriorityQueue<>((e1, e2) -> e2 - e1);
int count = 0;
while (!queue.isEmpty()) {
int k = n + 1;
List<Integer> next = new ArrayList<>();
while (!queue.isEmpty() && k > 0) {
int target = queue.poll();
if (target - 1 > 0) {
}
count++;
k--;
}
if (!next.isEmpty()) {
count += k;
}
}
return count;
}
}``````
``````public class Solution {
public int leastInterval(char[] tasks, int n) {
int[] val = new int[26];
for(int i = 0; i < tasks.length; i ++){