-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask_Scheduler.java
More file actions
50 lines (40 loc) · 1.76 KB
/
Copy pathTask_Scheduler.java
File metadata and controls
50 lines (40 loc) · 1.76 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
41
42
43
44
45
46
47
48
49
50
// 621. Task Scheduler
// 1. Use a priority queue to have task with highest frequency at front, arrange each task one time and decrement empty slots relatively,
// if there are still empty slots it will require such number of idles until next round, otherwise no idle needed
// (since the n limit has already passed)
// 2. get all emptyslots and all available tasks, idles = Math.max(0, emptySlots - availableTasks);
// 3. extension: 358. Rearrange String k Distance Apart
class Solution {
public int leastInterval(char[] tasks, int n) {
Map<Character, Integer> map = new HashMap<>();
for (char task : tasks) {
map.put(task, map.getOrDefault(task, 0) + 1);
}
PriorityQueue<Map.Entry<Character, Integer>> queue = new PriorityQueue<>(
(a, b) -> a.getValue() == b.getValue() ? b.getKey() - a.getKey() : b.getValue() - a.getValue());
queue.addAll(map.entrySet());
int count = 0;
while (!queue.isEmpty()) {
int emptySlots = n + 1;
List<Map.Entry> tempList = new ArrayList<>();
while (emptySlots > 0 && !queue.isEmpty()) {
Map.Entry<Character, Integer> task = queue.poll();
emptySlots--;
count++;
task.setValue(task.getValue() - 1);
if (task.getValue() > 0) {
tempList.add(task);
}
}
for (Map.Entry task : tempList) {
queue.add(task);
}
if (queue.isEmpty()) break;
if (emptySlots > 0) {
// idle
count += emptySlots;
}
}
return count;
}
}