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.

A CPU has to run a list of tasks. Each task is an uppercase letter from A to Z, and each task takes exactly one interval. In every interval the CPU either runs one task or stays idle. Tasks may run in any order, but two runs of the same task must have at least `n` intervals between them. Implement `least_interval(tasks, n)` so that it returns the minimum number of intervals needed to finish every task, idle intervals included. ### 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. The schedule itself is not part of the output. - The answer is at most 1,009,900, so it never exceeds 2^31-1 and fits in a 32-bit `int` in Java and C++. ### Example 1 ```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 on its own needs intervals 0, 3 and 6 at the tightest, and the two letters cannot share an interval, so one of them has to finish at interval 7 or later. ### Example 2 ```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 tasks need 16 intervals on their own (five gaps of three intervals, plus the last A), and the six other tasks fit into the gaps. ### Constraints - `1 <= len(tasks) <= 10^4` - `0 <= n <= 100` - Every `tasks[i]` is an uppercase English letter.

Constraints

  • 1 <= len(tasks) <= 10^4
  • 0 <= n <= 100
  • Every tasks[i] is a single uppercase English letter from A to Z.

Examples

Input: (['A', 'A', 'A', 'B', 'B', 'B'], 2)

Expected Output: 8

Explanation: Source example: A B idle A B idle A B. A and B tie at count 3, so (3-1)*3+2 = 8.

Input: (['A', 'A', 'A', 'B', 'B', 'B'], 0)

Expected Output: 6

Explanation: Source example with no cooldown: the tasks run back to back, so the answer is the task count 6.

Hints

  1. Look at the task kind that appears most often. How many intervals would it need if it were the only task?
  2. If several task kinds share the highest count, think about what the final stretch of the schedule must contain.
  3. Try a case with many different letters and a small cooldown, where the schedule may need no idle intervals at all.

Minimum CPU Intervals With a Per-Task Cooldown

mediumGeneralSoftware Engineer

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

Implement least_interval(tasks, n) so that it returns the minimum number of intervals needed to finish every task, idle intervals included.

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. The schedule itself is not part of the output.
  • The answer is at most 1,009,900, so it never exceeds 2^31-1 and fits in a 32-bit int in Java and C++.

Example 1

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 on its own needs intervals 0, 3 and 6 at the tightest, and the two letters cannot share an interval, so one of them has to finish at interval 7 or later.

Example 2

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 tasks need 16 intervals on their own (five gaps of three intervals, plus the last A), and the six other tasks fit into the gaps.

Constraints

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

Examples

Example 1
Input
(['A', 'A', 'A', 'B', 'B', 'B'], 2)
Output
8
Notes
Source example: A B idle A B idle A B. A and B tie at count 3, so (3-1)*3+2 = 8.
Example 2
Input
(['A', 'A', 'A', 'B', 'B', 'B'], 0)
Output
6
Notes
Source example with no cooldown: the tasks run back to back, so the answer is the task count 6.

Constraints

  • •1 <= len(tasks) <= 10^4
  • •0 <= n <= 100
  • •Every tasks[i] is a single uppercase English letter from A to Z.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def least_interval(tasks: list[str], n: int) -> int:
Sample call
least_interval(['A', 'A', 'A', 'B', 'B', 'B'], 2)
Language
Case 1
Input
(['A', 'A', 'A', 'B', 'B', 'B'], 2)
Expected
8
Case 2
Input
(['A', 'A', 'A', 'B', 'B', 'B'], 0)
Expected
6

Browse More Questions

Show the approach

Approach

Count how many times each of the 26 letters appears. Let m be the highest count and k the number of letters that reach it. The answer is max(len(tasks), (m-1)*(n+1)+k).

Two lower bounds hold for every valid schedule. Every task needs its own interval, so a schedule has at least len(tasks) intervals. Each of the k most frequent letters runs m times with at least n+1 intervals between consecutive runs, so its last run comes at least (m-1)(n+1) intervals after its first. The earliest of those k last runs is therefore at interval (m-1)(n+1) or later, and since the k last runs occupy k different intervals, the latest of them is at interval (m-1)(n+1)+k-1 or later. That forces (m-1)(n+1)+k intervals.

The larger of the two bounds can always be reached. Build m frames: the first m-1 frames have n+1 slots each and the last frame holds just the k most frequent letters. Put those k letters at the start of every frame. Then place the remaining letters, in order of decreasing count, column by column through the first m-1 frames. Each remaining letter appears at most m-1 times, so its copies land in different frames and stay at least n+1 intervals apart; unused slots become idle. If the remaining letters do not fit, widen the frames: runs only move further apart, no slot is idle, and the schedule uses exactly len(tasks) intervals.

Edge cases: a single task gives 1. With n = 0 the frame term (m-1)+k never exceeds len(tasks), because len(tasks) >= m*k >= m+k-1, so the answer is the task count. When every letter appears once, m = 1 and both terms equal len(tasks). The largest possible answer, 1,009,900, comes from 10^4 copies of one letter with n = 100, so 32-bit integers are enough in every language.

Time complexity:
O(N), where N = len(tasks)
Space complexity:
O(1)