Ctrl K

Jobs With a Cooldown

Medium
A machine has to run the jobs in jobs; each job is labelled with an uppercase English letter, and equal letters mean jobs of the same kind. In one time slot the machine either runs one job or stays idle. Jobs may be run in any order. After a job of some kind the machine needs to cool down: at least cooldown slots must pass between two jobs of the same kind. Return the smallest number of slots needed to run every job.
Example 1
jobs=["X", "X", "X", "Y"], cooldown=1 → 5

For example X Y X _ X: runs of X need at least one slot between them, so one slot sits idle. 5 in total.

Example 2
jobs=["P", "Q", "P", "R", "Q", "S"], cooldown=1 → 6

The order P Q R P S Q needs no idle slots.

Example 3
jobs=["K", "K", "K"], cooldown=3 → 9

K _ _ _ K _ _ _ K takes 9 slots.

Constraints
  • 1 ≤ len(jobs) ≤ 10⁴
  • jobs[i] is an uppercase English letter
  • 0 ≤ cooldown ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSpread Out the Labels MediumMost Common Values MediumMost Searched Words MediumNo Twin Letters Side by Side MediumPick Within Tag Limits Medium
loading Python…
Input
jobs=["X", "X", "X", "Y"], cooldown=1
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 89 tests on the server, hidden ones included.