Skip to content
RED SAGSkills
medium30 ptsgreedycountingheap

CPU Jobs with Cooldown

A CPU must run a list of jobs tasks, where each job is a single uppercase letter naming its type. Every job takes one time unit. After running a job of some type, the CPU must wait at least n time units before running another job of the same type. During a unit the CPU either runs one job or sits idle, and jobs may run in any order.

Return the smallest number of time units needed to finish all jobs.

Example 1

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

Example 2

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

Constraints

  • 0 <= tasks.length <= 10^4
  • tasks[i] is an uppercase letter
  • 0 <= n <= 100
Hint 1

The most frequent type decides the skeleton: (max - 1) blocks of length n + 1, plus a final block.

Hint 2

Types that tie for the maximum count each add one slot to the final block. The answer is never less than the number of jobs.

Good practice for: Backend Developer, Site Reliability Engineer, Embedded Systems Engineer, Cloud Engineer, DevOps Engineer

Run checks your code on 3 sample tests. Submit runs all 9 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.