InferenceAI

Enter at least two characters to search.

Start free

Cooldown Scheduler

mediumHeaps25 min25 XP

A CPU runs one task per time unit. Identical tasks (same letter) must be at least n units apart; the CPU may idle. Return the minimum number of time units needed to run every task in tasks.

The answer has a closed form. With ff the highest task count and cc the number of tasks with that count:

T=max⁡(∣tasks∣,  (f−1)(n+1)+c)T = \max\big(|\text{tasks}|,\; (f - 1)(n + 1) + c\big)

Examples

EXAMPLE WALKTHROUGHidle needed

A guided view of the published example, not a trace of your code.

INPUT[["A","A","A","B","B","B"],2]
TASKcooldown_scheduler()
EXPECTED OUTPUT8

Start with the input values shown in this example.

Example 1 of 3

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

Output: 8

Explanation: idle needed

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

Output: 6

Explanation: no cooldown

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

Output: 16

Explanation: one dominant task

Hint 1

The most frequent task sets the skeleton: f - 1 full blocks of length n + 1, then a final partial block.

Topics

DSAHeapsPython