Task Scheduler
Try to solve the Task Scheduler problem.
We'll cover the following
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:
tasks.length
n
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:
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
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
3
7
8
9
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to better understand how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.
import java.util.*;class TaskScheduler {public static int leastTime(char[] tasks, int n) {// Replace this placeholder return statement with your codereturn -1;}}