FastPrepTask Scheduler with Cooldown

Task Scheduler with Cooldown

SambaNova Systems logoSambaNova Systems● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Each task takes one time unit. You may execute tasks in any order or remain idle, but two executions of the same task ID must have at least cooldown intervening time units.

Return the minimum total number of time units needed to execute every task.

Function

leastInterval(tasks: String[], cooldown: int) → int

Examples

Example 1

tasks = ["A","A","A","B","B","B"]cooldown = 2return = 8

A B idle A B idle A B is optimal.

Example 2

tasks = ["A","A","A","B","B","B"]cooldown = 0return = 6

No cooldown requires no idle time.

Example 3

tasks = ["A","A","A","B","B","B","C","C"]cooldown = 2return = 8

C tasks fill both idle positions.

Constraints

  • 1 <= tasks.length <= 100000.
  • Task IDs are non-empty uppercase ASCII strings of at most 20 characters.
  • 0 <= cooldown <= 100000.

More SambaNova Systems problems

See SambaNova Systems hiring insights
public int leastInterval(String[] tasks, int cooldown) {
    // write your code here
}
tasks["A","A","A","B","B","B"]
cooldown2
expected8
Checking account…