Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-108

Task Scheduler

Problem

Given task labels and a cooldown n that must separate two runs of the same task, return the minimum number of time slots needed to run all tasks.

Let max_count be the highest frequency and num_max how many tasks share it; the answer is the larger of the task count and (max_count - 1) * (n + 1) + num_max. Return 0 when there are no tasks.

Hint

The most frequent task defines the frame; idle slots can be filled by other tasks.

Test cases

case 1
in: [["A","A","A","B","B","B"],2]
expected: 8
case 2
in: [["A","A","A","B","B","B"],0]
expected: 6
case 3
in: [["A","A","A","A","A","A","B","C","D","E","F","G"],2]
expected: 16
case 4
in: [["A"],3]
expected: 1
case 5
in: [["A","A","A"],3]
expected: 9
Starter codePython
def least_interval(tasks, n):
    # Your code here
    pass

5 test cases · run your code against them in the editor