Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Greedy
Heaps
Google
Amazon
Task Scheduler

Schedule tasks with a cooldown so identical tasks are separated by at least n intervals, and compute the minimum total time needed.

Acceptance 0%
Problem Statement

You are given an array of tasks, where each task is represented by a capital letter, and an integer cooldown period n.

In one unit of time, you may either execute one task or stay idle. The same task type cannot be executed again until at least n other time units have passed since its last execution.

Return the minimum number of time units required to finish all tasks.

The goal is to arrange the task executions so that the cooldown constraint is satisfied while minimizing idle time.

Input Format

  • tasks: an array of task labels, typically uppercase letters.
  • n: a non-negative integer cooldown interval.

Output Format

Return a single integer: the minimum number of time units needed to complete all tasks.

Constraints

  • 1 <= tasks.length
  • tasks[i] is a task label.
  • 0 <= n
  • The answer fits in a 32-bit signed integer.
Examples
Sample cases returned by the problem API.

Example 1

Input

tasks = ["A","A","A","B","B","B"], n = 2

Output

8

Explanation

One optimal schedule is A -> B -> idle -> A -> B -> idle -> A -> B, which takes 8 time units.

Example 2

Input

tasks = ["A","A","A","B","B","B"], n = 0

Output

6

Explanation

No cooldown is required, so the tasks can be executed back to back in any order.

Show 1 more example

Example 3

Input

tasks = ["A","A","A","B","B","B"], n = 1

Output

6

Explanation

One valid schedule is A -> B -> A -> B -> A -> B, which uses no idle time.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.