Skip to content

Practice Task Scheduler

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#621
LeetCode Problem

Task Scheduler

Problem Statement

You are given an array of CPU tasks, each labeled with a letter from A to Z, and a number n. Each CPU interval can be idle or allow the completion of one task. Tasks can be completed in any order, but there's a constraint: there has to be a gap of at least n intervals between two tasks with the same label. Return the minimum number of CPU intervals required to complete all tasks. Practice approach: Let f be the largest label frequency and c the number of labels occurring f times. The minimum duration is max(number of tasks, (f - 1) * (n + 1) + c). The first f - 1 occurrences of a most-frequent label each need a block of at least n + 1 intervals; the final block needs room for all c equally frequent labels. Other tasks fill the gaps and may make the task count itself the tighter bound. For three A tasks and three B tasks with n = 2, A B idle A B idle A B takes eight intervals. With two each of A, B, and C and n = 1, A B C A B C uses all six intervals without idling. Count frequencies in O(number of tasks) time with 26 counters.

Source: Task Scheduler on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
tasks = ["A","A","A","B","B","B"], n = 2
Output
8

# Example 2

Input
tasks = ["A","C","A","B","D","B"], n = 1
Output
6

# Example 3

Input
tasks = ["A","A","A", "B","B","B"], n = 3
Output
10

Constraints

1 <= tasks.length <= 10^4
tasks[i] is an uppercase English letter.
0 <= n <= 100

Related LeetCode Problems

Practice more problems similar to Task Scheduler.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.