Task Scheduler

You are given an array of CPU tasks, each labeled with a letter from A to Z, and a number n. Each CPU interval can be idle or allow the completion of one task.

Tasks can be completed in any order, but there is a constraint: there has to be a gap of at least n intervals between two tasks with the same label.

Return the minimum number of CPU intervals required to complete all tasks.

Example 1
Inputtasks = ["A","A","A","B","B","B"], n = 2
Output8
A possible sequence is A -> B -> idle -> A -> B -> idle -> A -> B, for a total of 8 intervals.
Example 2
Inputtasks = ["A","C","A","B","D","B"], n = 1
Output6
With a cooling interval of 1, the tasks can be completed as A -> B -> C -> D -> A -> B with no idle intervals.

Constraints

  • 1 <= tasks.length <= 10^4
  • tasks[i] is an uppercase English letter.
  • 0 <= n <= 100

Asked at 25 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate