FastPrepTask Scheduler with Cooldown

Task Scheduler with Cooldown

GoDaddy logoGoDaddy● MediumFULLTIMEOA

Problem statement

The string tasks lists tasks waiting for one processor. Each uppercase letter is a task type, and each occurrence must run exactly once.

You may choose any execution order. Each task takes one time unit, and the processor runs at most one task per time unit. It may stay idle.

Between two executions of the same type, at least cooldown whole time units must pass. Other task types or idle time can fill those units. For example, if cooldown = 2, tasks of one type may start at times 0 and 3, but not at times 0 and 2.

Return the minimum total number of time units needed to finish every task, including idle time. The answer may be larger than a signed 32-bit integer.

Function

leastInterval(tasks: String, cooldown: int) → long

Examples

Example 1

tasks = "AAABBB"cooldown = 2return = 8

One optimal schedule is A, B, idle, A, B, idle, A, B. Each repeated type has two time units between executions. The three copies of each of two types require at least (3 - 1) * (2 + 1) + 2 = 8 time units.

Example 2

tasks = "AAABC"cooldown = 1return = 5

The schedule A, B, A, C, A has no idle time and separates consecutive A tasks by one time unit. Five tasks cannot finish in fewer than five time units.

Constraints

  • 1 <= tasks.length <= 100000.
  • tasks contains only the uppercase English letters A through Z.
  • 0 <= cooldown <= 100000.

More GoDaddy problems

See GoDaddy hiring insights
Saved
public long leastInterval(String tasks, int cooldown) {
    // write your code here
}
Checking account…