Find the minimum CPU intervals to run tasks with a cooldown between identical tasks

Quick Overview

Given tasks labeled by uppercase letters and a cooldown n between runs of the same task, compute the fewest CPU intervals needed to finish them all, idle slots included. It tests greedy reasoning from task counts, proving that a lower bound can be reached, and edge cases such as a zero cooldown.

Find the minimum CPU intervals to run tasks with a cooldown between identical tasks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A CPU has to run a list of tasks. Each task is an uppercase letter from A to Z, and each one takes exactly one interval. In every interval the CPU either runs one task or sits idle. The tasks can run in any order, but two runs of the same task must have at least `n` intervals between them. Return the minimum number of intervals needed to finish every task, idle intervals included. ### Function Signature ```python def least_interval(tasks: list[str], n: int) -> int: ``` ### Rules - `tasks` is a list of single-character strings. Equal letters are the same kind of task. - If a task runs at interval `i`, the next run of the same task can happen at interval `i + n + 1` at the earliest. - Return only the length of the shortest valid schedule, not the schedule itself. ### Constraints - `1 <= len(tasks) <= 10^4` - `0 <= n <= 100` - Every `tasks[i]` is an uppercase English letter. ### Examples ```text Input: tasks = ["A","A","A","B","B","B"], n = 2 Output: 8 ``` One shortest order is `A B idle A B idle A B`. Each letter alone needs slots 0, 3 and 6 at the tightest, and the two letters cannot share slots, so one of them has to finish at slot 7 or later. ```text Input: tasks = ["A","A","A","B","B","B"], n = 0 Output: 6 ``` With no cooldown the tasks run back to back, so the answer is the number of tasks. ```text Input: tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2 Output: 16 ``` One shortest order is `A B C A D E A F G A idle idle A idle idle A`. The six A's need 16 slots on their own (five gaps of three slots, plus the last A), and the six other tasks fit into the gaps.

Overview: Given tasks labeled by uppercase letters and a cooldown n between runs of the same task, compute the fewest CPU intervals needed to finish them all, idle slots included. It tests greedy reasoning from task counts, proving that a lower bound can be reached, and edge cases such as a zero cooldown.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A CPU has to run a list of tasks. Each task is an uppercase letter from A to Z, and each one takes exactly one interval. In every interval the CPU either runs one task or sits idle. The tasks can run in any order, but two runs of the same task must have at least n intervals between them.

Return the minimum number of intervals needed to finish every task, idle intervals included.

Function Signature

def least_interval(tasks: list[str], n: int) -> int:

Rules

  • tasks is a list of single-character strings. Equal letters are the same kind of task.
  • If a task runs at interval i , the next run of the same task can happen at interval i + n + 1 at the earliest.
  • Return only the length of the shortest valid schedule, not the schedule itself.

Constraints

  • 1 <= len(tasks) <= 10^4
  • 0 <= n <= 100
  • Every tasks[i] is an uppercase English letter.

Examples

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

One shortest order is A B idle A B idle A B. Each letter alone needs slots 0, 3 and 6 at the tightest, and the two letters cannot share slots, so one of them has to finish at slot 7 or later.

Input: tasks = ["A","A","A","B","B","B"], n = 0
Output: 6

With no cooldown the tasks run back to back, so the answer is the number of tasks.

Input: tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
Output: 16

One shortest order is A B C A D E A F G A idle idle A idle idle A. The six A's need 16 slots on their own (five gaps of three slots, plus the last A), and the six other tasks fit into the gaps.