Menu
CoddyTech

Task Scheduler

You get a string tasks of uppercase letters, one letter per task, and an integer n. A processor runs one task per unit of time, in any order you choose, or stays idle for a unit.

Two runs of the same letter need at least n units between them: a letter that runs at unit t can run again at unit t+n+1 at the earliest. Return the least number of units that runs every task.

Function

leastInterval(tasks: string, n: integer) → integer
tasksstring
the tasks, one uppercase letter each
ninteger
the least number of units between two runs of the same letter
Returnsinteger
the least number of units, idle ones included

Constraints

  • 1 ≤ tasks.length ≤ 104
  • tasks contains only the uppercase letters A to Z.
  • 0 ≤ n ≤ 100

Examples

Input
tasks = "CACBAC"n = 2
Output
7
Explanation
C runs three times with two units between runs, so its runs alone span 7 units. The order C A B C A, idle, C fills the gaps with A and B and needs one idle unit: 7.
challenge icon

Follow-up

Can you also return one schedule of the least length, as a string with a . for each idle unit?

Reset code
def leastInterval(tasks, n):
    # Write code here

Test #1

Test #2

Test #3

Arguments

tasks = "CACBAC"
n = 2

Expected Output

7