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.
eg:
input (tasks, n) | output |
---|---|
tasks = ["A","A","A","B","B","B"], n = 2 | 8 |
tasks = ["A","C","A","B","D","B"], n = 1 | 6 |
Here's how we can implement this in Java:
import java.util.Arrays;
class Task {
public int func(char[] tasks, int n) {
// Step 1: Count frequency of each task
int[] fr = new int[26];
for (int i = 0; i < tasks.length; i++)
fr[tasks[i] - 'A']++;
// Step 2: Sort the frequencies in ascending order
Arrays.sort(fr);
// Step 3: Calculate the basic frame based on the highest frequency
int space = fr[25] - 1;
int gap = space * n;
// Step 4: Fill the gaps with other tasks
for (int i = 24; i >= 0; i--) {
gap -= Math.min(space, fr[i]);
}
// Step 5: Calculate the total intervals required
return gap < 0 ? tasks.length : gap + tasks.length;
}
public static void main(String[] args) {
Task scheduler = new Task();
char[] tasks1 = {'A', 'A', 'A', 'B', 'B', 'B'};
int n1 = 2;
System.out.println(scheduler.func(tasks1, n1)); // Output: 8
char[] tasks2 = {'A', 'C', 'A', 'B', 'D', 'B'};
int n2 = 1;
System.out.println(scheduler.func(tasks2, n2)); // Output: 6
}
}
More info
- Count Frequency of Tasks:
- The array
fr
of size 26 is used to store the frequency of each task (assuming tasks are represented by uppercase English letters 'A' to 'Z'). - Iterate over the
tasks
array and increment the corresponding index in thefr
array.
- The array
- Sort the Frequencies:
- Sort the
fr
array in ascending order. This makes it easier to identify the task with the maximum frequency and handle the scheduling.
- Sort the
- Calculate the Basic Frame:
- The variable
space
is calculated asfr[25] - 1
, wherefr[25]
is the maximum frequency of any task. - The
gap
is calculated asspace * n
. This represents the total number of idle slots needed if we only consider the most frequent task.
- The variable
- Fill the Gaps with Other Tasks:
- Iterate over the remaining frequencies in descending order (from
fr[24]
tofr[0]
). - Reduce the
gap
by the lesser ofspace
or the current frequency. This step essentially tries to fill the idle slots with other tasks as much as possible.
- Iterate over the remaining frequencies in descending order (from
- Calculate the Total Intervals:
- If
gap
is less than 0, it means that all the slots can be filled without any idle times, so the result is the length of thetasks
array. - Otherwise, the result is the total number of slots needed (
gap + tasks.length
).
- If
Example 1: tasks = ["A", "A", "A", "B", "B", "B"], n = 2
- Count Frequency of Tasks:
fr = [3, 3, 0, 0, ..., 0]
(frequencies of A and B are 3, others are 0)
- Sort the Frequencies:
fr = [0, 0, ..., 0, 3, 3]
- Calculate the Basic Frame:
space = fr[25] - 1 = 3 - 1 = 2
gap = space * n = 2 * 2 = 4
- Fill the Gaps with Other Tasks:
gap -= Math.min(space, fr[24]) = gap - Math.min(2, 3) = 4 - 2 = 2
- Continue filling gaps:
gap -= Math.min(space, fr[23]) = gap - Math.min(2, 0) = 2
- (No more tasks to fill, so
gap
remains 2)
- Calculate the Total Intervals:
- Since
gap
is not less than 0, the result isgap + tasks.length = 2 + 6 = 8
Output: 8
Example 2: tasks = ["A", "C", "A", "B", "D", "B"], n = 1
- Count Frequency of Tasks:
fr = [2, 2, 1, 1, 0, ..., 0]
(frequencies of A, B are 2, C and D are 1)
- Sort the Frequencies:
fr = [0, 0, ..., 1, 1, 2, 2]
- Calculate the Basic Frame:
space = fr[25] - 1 = 2 - 1 = 1
gap = space * n = 1 * 1 = 1
- Fill the Gaps with Other Tasks:
gap -= Math.min(space, fr[24]) = gap - Math.min(1, 2) = 1 - 1 = 0
- Continue filling gaps:
gap -= Math.min(space, fr[23]) = gap - Math.min(1, 1) = 0
- (No more gaps to fill, so
gap
remains 0)
- Calculate the Total Intervals:
- Since
gap
is less than 0, the result istasks.length = 6
Output: 6
- Time complexity:O(n)
- Space complexity:O(1)