Search⌘ K
AI Features

Task Scheduler

Explore how to manage CPU tasks with cooling periods by learning to track and schedule tasks optimally. This lesson helps you understand and implement solutions for minimizing CPU intervals when tasks require separation, a common coding interview pattern.

Statement

You are given an array of CPU tasks represented by uppercase letters (A to Z) and an integer n, which denotes the cooling period required between any two identical tasks. Each task takes exactly one CPU interval to execute. Therefore, each CPU interval can either perform a task or remain idle. Tasks can be executed in any order, but the same task must be separated by at least n intervals.

Determine the minimum number of CPU intervals required to complete all tasks.

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:

canvasAnimation-image
1 / 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


1 / 3

Figure it out!

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

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

1
2
3
4

Try it yourself

Implement your solution in the following coding playground.

JavaScript
usercode > main.js
function leastInterval(tasks, n){
// Replace this placeholder return statement with your code
return -1
}
export {
leastInterval
};
Task Scheduler