forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskScheduler_621.java
More file actions
40 lines (37 loc) · 1.11 KB
/
TaskScheduler_621.java
File metadata and controls
40 lines (37 loc) · 1.11 KB
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
package LeetCode;
import java.util.*;
/**
* @Classname TaskScheduler_621
* @Description 同一种任务之间必须间隔n秒,求出最少需要的时间,中间间隔不够的使用哑间隔进行填充!
* @Date 19-5-14 下午9:15
* @Created by mao<[email protected]>
*/
public class TaskScheduler_621 {
public int leastInterval(char[] tasks, int n) {
Map<Character,Integer> count=new HashMap<>();
for(int i=0;i<tasks.length;i++){
if(count.containsKey(tasks[i])){
int value=count.get(tasks[i])+1;
count.put(tasks[i],value);
}else{
count.put(tasks[i],1);
}
}
int len=count.size();
ArrayList<Integer> result=new ArrayList<>();
for(Character key:count.keySet()){
result.add(count.get(key));
}
Collections.sort(result);
int a=1;
int b=result.get(len-1);
for(int j=len-2;j>=0;j--){
if(result.get(j)==b){
a++;
}else {
break;
}
}
return a*b+(n-b+1)*(a-1);
}
}