comments | difficulty | edit_url | tags | ||||||
---|---|---|---|---|---|---|---|---|---|
true |
Medium |
|
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.
After completing task A, you must wait two cycles before doing A again. The same applies to task B. In the 3rd interval, neither A nor B can be done, so you idle. By the 4th cycle, you can do A again as 2 intervals have passed.
Example 2:
Input: tasks = ["A","C","A","B","D","B"], n = 1
Output: 6
Explanation: A possible sequence is: A -> B -> C -> D -> A -> B.
With a cooling interval of 1, you can repeat a task after just one other task.
Example 3:
Input: tasks = ["A","A","A", "B","B","B"], n = 3
Output: 10
Explanation: A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.
There are only two types of tasks, A and B, which need to be separated by 3 intervals. This leads to idling twice between repetitions of these tasks.
Constraints:
1 <= tasks.length <= 104
tasks[i]
is an uppercase English letter.0 <= n <= 100
class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
cnt = Counter(tasks)
x = max(cnt.values())
s = sum(v == x for v in cnt.values())
return max(len(tasks), (x - 1) * (n + 1) + s)
class Solution {
public int leastInterval(char[] tasks, int n) {
int[] cnt = new int[26];
int x = 0;
for (char c : tasks) {
c -= 'A';
++cnt[c];
x = Math.max(x, cnt[c]);
}
int s = 0;
for (int v : cnt) {
if (v == x) {
++s;
}
}
return Math.max(tasks.length, (x - 1) * (n + 1) + s);
}
}
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
vector<int> cnt(26);
int x = 0;
for (char c : tasks) {
c -= 'A';
++cnt[c];
x = max(x, cnt[c]);
}
int s = 0;
for (int v : cnt) {
s += v == x;
}
return max((int) tasks.size(), (x - 1) * (n + 1) + s);
}
};
func leastInterval(tasks []byte, n int) int {
cnt := make([]int, 26)
x := 0
for _, c := range tasks {
c -= 'A'
cnt[c]++
x = max(x, cnt[c])
}
s := 0
for _, v := range cnt {
if v == x {
s++
}
}
return max(len(tasks), (x-1)*(n+1)+s)
}
public class Solution {
public int LeastInterval(char[] tasks, int n) {
int[] cnt = new int[26];
int x = 0;
foreach (char c in tasks) {
cnt[c - 'A']++;
x = Math.Max(x, cnt[c - 'A']);
}
int s = 0;
foreach (int v in cnt) {
s = v == x ? s + 1 : s;
}
return Math.Max(tasks.Length, (x - 1) * (n + 1) + s);
}
}