Task Scheduler

Try to solve the Task Scheduler problem.

Statement

Given a character array tasks, where each character represents a unique task and a positive integer n that represents the cooling period between any two identical tasks, find the minimum number of time units the CPU will need to complete all the given tasks. Each task requires one unit to perform, and the CPU must wait for at least n units of time before it can repeat the same task. During the cooling period, the CPU can perform other tasks or remain idle.

Constraints:

  • 1 1 \leq  tasks.length 1000 \leq 1000

  • 00 \le n 100 \leq 100

  • tasks consists of uppercase English letters

Examples

Let’s take a look at a few examples to get a better understanding of the problem statement:

Press + to interact
canvasAnimation-image
1 of 3

Understand the problem

Let’s take a moment to ensure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem.

Task Scheduler

1

What’s the minimum number of units of time if the following values are given as input?

Tasks = [A, A, A, B, B, C, C]

n = 1

A)

3

B)

7

C)

8

D)

9

Question 1 of 30 attempted

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to better understand how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Try it yourself

Implement your solution in the following coding playground.

Press + to interact
Java
usercode > TaskScheduler.java
import java.util.*;
class TaskScheduler {
public static int leastTime(char[] tasks, int n) {
// Replace this placeholder return statement with your code
return -1;
}
}
Task Scheduler