-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathProblem_13_Task_Scheduler.java
More file actions
40 lines (33 loc) · 1.53 KB
/
Problem_13_Task_Scheduler.java
File metadata and controls
40 lines (33 loc) · 1.53 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 Top_K_Elements;
// Problem Statement: Scheduling Tasks (Hard)
// LeetCode Question: 621. Task Scheduler
import java.util.*;
public class Problem_13_Task_Scheduler {
public int scheduleTasks(char[] tasks, int k) {
int intervalCount = 0;
Map<Character, Integer> taskFrequencyMap = new HashMap<>();
for (char chr : tasks)
taskFrequencyMap.put(chr, taskFrequencyMap.getOrDefault(chr, 0) + 1);
PriorityQueue<Map.Entry<Character, Integer>> maxHeap =
new PriorityQueue<Map.Entry<Character, Integer>>(
(e1, e2) -> e2.getValue() - e1.getValue());
// add all tasks to the max heap
maxHeap.addAll(taskFrequencyMap.entrySet());
while (!maxHeap.isEmpty()) {
List<Map.Entry<Character, Integer>> waitList = new ArrayList<>();
int n = k + 1; // try to execute as many as 'k+1' tasks from the max-heap
for (; n > 0 && !maxHeap.isEmpty(); n--) {
intervalCount++;
Map.Entry<Character, Integer> currentEntry = maxHeap.poll();
if (currentEntry.getValue() > 1) {
currentEntry.setValue(currentEntry.getValue() - 1);
waitList.add(currentEntry);
}
}
maxHeap.addAll(waitList); // put all the waiting list back on the heap
if (!maxHeap.isEmpty())
intervalCount += n; // we'll be having 'n' idle intervals for the next iteration
}
return intervalCount;
}
}