LC 621 — Problem

Given a list of tasks (each labeled by a letter) and a positive integer n, schedule the tasks on a single CPU. Between two occurrences of the SAME task, the CPU must rest for at least n slots (it may do other tasks during cooldown, or sit idle). Return the MINIMUM total number of time units needed to finish all tasks.

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

Constraints: 1 ≤ tasks.length ≤ 10^4 · tasks[i] uppercase English letter · 0 ≤ n ≤ 100.

Phase 1: Can you beat 10 time units?

Can You Beat Ten?

Eight tasks: [A, A, A, B, B, B, C, C]. Between two occurrences of the SAME task, the CPU needs a cooldown of n=2 slots — that is, two slots in which the CPU must do something else (or sit idle). Schedule them. How few time units can you pull this off in?

FIG. 1 — TASKS TO SCHEDULE
cooldownn = 2

How many time slots does the optimal schedule need?