Skip to content
Onsite

Task Scheduler

MediumP2Coding

You are given an array tasks of single-letter task labels and a cooling interval n. Each task takes one unit of time, and the CPU can run one task or sit idle in each unit. Two runs of the same label must be separated by at least n units in between. Tasks may be run in any order.

Return the minimum number of time units needed to finish all tasks.

Example 1

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

Example 2

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

Constraints

  • 1 <= tasks.length <= 10^4
  • tasks[i] is an upper-case English letter
  • 0 <= n <= 100

Hints

Run checks the 3 visible tests, Submit all 10.