Intermediate Level

Implement the taskSchedulerIntervals method that returns the minimum intervals required to schedule tasks with cooldown.

The input contains a string tasks and an integer cooldown. Each character represents one task type.

The same task type must have at least cooldown intervals between two executions. Different task types can be executed without waiting.

Your task is to return the minimum number of intervals needed to complete all tasks. Idle intervals may be required if no valid task can be executed.

Example 1

Input
tasks (string) = AAABBB
cooldown (int) = 2
Returns (int) 8

Example 2

Input
tasks (string) = AAABBB
cooldown (int) = 0
Returns (int) 6

Example 3

Input
tasks (string) = AAABC
cooldown (int) = 2
Returns (int) 7

Use the frequency of the most common task to calculate the minimum schedule length.

The most frequent task creates gaps that must be filled by other tasks or idle intervals. If the maximum frequency is maxFreq, it creates maxFreq - 1 full blocks of size cooldown + 1. Add the number of tasks that share the maximum frequency for the last block.

The answer is the larger value between this calculated frame size and the total number of tasks.

Pseudocode:

function taskSchedulerIntervals(tasks, cooldown):
    frequency = count each task character
    maxFreq = largest frequency
    maxCount = number of tasks with frequency maxFreq
    frameSize = (maxFreq - 1) * (cooldown + 1) + maxCount
    answer = maximum(length of tasks, frameSize)
    return answer
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.