-
Notifications
You must be signed in to change notification settings - Fork 1
/
TaskScheduler.java
68 lines (56 loc) · 2.21 KB
/
TaskScheduler.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.leetcode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
/**
You are given an array of CPU tasks, each represented by letters A to Z, and a cooling time, n.
Each cycle or interval allows the completion of one task. Tasks can be completed in any order, but there's a constraint: identical tasks must be separated by at least n intervals due to cooling time.
Return the minimum number of intervals required to complete all tasks.
Example 1:
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B.
**/
public class TaskScheduler {
class Solution {
//Complete the task type(A-Z) with max count first followed by second most
//complete n such operations unless we encounter first task type. if incounter add n time
//to check encounter or not better poll the task from DS to another DS.
//We only require count if we are using 2 datascructure to see visited and to be done
public int leastInterval(char[] tasks, int n) {
if(tasks == null || tasks.length == 0) return 0;
HashMap<Character, Integer> map = new HashMap<>();
for(char ch : tasks){
map.put(ch , map.getOrDefault(ch,0) +1);
}
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a,b)->(b-a));
for(char ch : map.keySet()){
maxHeap.offer(map.get(ch));
}
int result = 0;
while(maxHeap.size() > 0){
List<Integer> nextCycle = new ArrayList<>();
int cycle = n+1;
while(cycle > 0 && maxHeap.size() > 0){
int t = maxHeap.poll();
t--;
cycle--;
if(t>0){
nextCycle.add(t);
}
}
if(nextCycle.size() == 0){
result+= n+1-cycle;
}else{
result += n+1;
}
for(int t : nextCycle){
maxHeap.offer(t);
}
}
return result;
}
}
}