-
Notifications
You must be signed in to change notification settings - Fork 3
/
_0621_TaskScheduler.java
110 lines (105 loc) · 3.86 KB
/
_0621_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.diguage.algorithm.leetcode;
import java.util.*;
/**
* = 621. Task Scheduler
*
* https://leetcode.com/problems/task-scheduler/[Task Scheduler - LeetCode]
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-01-31 21:26
*/
public class _0621_TaskScheduler {
/**
* Runtime: 2 ms, faster than 99.97% of Java online submissions for Task Scheduler.
* Memory Usage: 42.7 MB, less than 5.88% of Java online submissions for Task Scheduler.
*
* Copy from: https://leetcode-cn.com/problems/task-scheduler/solution/621-ren-wu-diao-du-qi-java-jie-ti-zhu-shi-ying-gai/[621. 任务调度器--Java--解题注释应该能看懂 - 任务调度器 - 力扣(LeetCode)]
*/
public int leastInterval(char[] tasks, int n) {
if (Objects.isNull(tasks) || tasks.length == 0) {
return 0;
}
int[] counts = new int[26];
for (char task : tasks) {
counts[task - 'A']++;
}
Arrays.sort(counts);
int maxCount = counts[25];
int restCount = (maxCount - 1) * (n + 1) + 1;
for (int i = 24; i >= 0 && counts[i] == maxCount; i--) {
restCount++;
}
return Math.max(restCount, tasks.length);
}
/**
* Runtime: 2 ms, faster than 99.97% of Java online submissions for Task Scheduler.
* Memory Usage: 42.6 MB, less than 5.88% of Java online submissions for Task Scheduler.
* <p>
* Copy from: https://leetcode-cn.com/problems/task-scheduler/solution/ren-wu-diao-du-qi-by-leetcode/[任务调度器 - 任务调度器 - 力扣(LeetCode)]
*/
public int leastInterval1(char[] tasks, int n) {
if (Objects.isNull(tasks) || tasks.length == 0) {
return 0;
}
int[] counts = new int[26];
for (char task : tasks) {
counts[task - 'A']++;
}
Arrays.sort(counts);
int maxValue = counts[25] - 1;
int idleSlots = maxValue * n;
for (int i = 24; i >= 0 && counts[i] > 0; i--) {
idleSlots -= Math.min(maxValue, counts[i]);
}
return idleSlots > 0 ? idleSlots + tasks.length : tasks.length;
}
/**
* Runtime: 29 ms, faster than 42.49% of Java online submissions for Task Scheduler.
* Memory Usage: 41.9 MB, less than 5.88% of Java online submissions for Task Scheduler.
* <p>
* Copy from: https://leetcode-cn.com/problems/task-scheduler/solution/ren-wu-diao-du-qi-by-leetcode/[任务调度器 - 任务调度器 - 力扣(LeetCode)]
*/
public int leastIntervalQueue(char[] tasks, int n) {
if (Objects.isNull(tasks) || tasks.length == 0) {
return 0;
}
int[] counts = new int[26];
for (char c : tasks) {
counts[c - 'A']++;
}
PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
for (int count : counts) {
if (count > 0) {
queue.add(count);
}
}
int time = 0;
while (!queue.isEmpty()) {
List<Integer> temp = new ArrayList<>();
int i = 0;
while (i <= n) {
if (!queue.isEmpty()) {
if (queue.peek() > 1) {
temp.add(queue.poll() - 1);
} else {
queue.poll();
}
}
time++;
i++;
if (queue.isEmpty() && temp.isEmpty()) {
break;
}
}
for (Integer count : temp) {
queue.add(count);
}
}
return time;
}
public static void main(String[] args) {
_0621_TaskScheduler solution = new _0621_TaskScheduler();
int r1 = solution.leastInterval("AAABBB".toCharArray(), 2);
System.out.println((r1 == 8) + " : " + r1);
}
}