Task Scheduler II

You are given a 0-indexed array of positive integers tasks, representing tasks that need to be completed in order, where tasks[i] represents the type of the i^th task.

You are also given a positive integer space, which represents the minimum number of days that must pass after the completion of a task before another task of the same type can be performed.

Each day, until all tasks have been completed, you must either:

  • Complete the next task from tasks, or
  • Take a break.

Return the minimum number of days needed to complete all tasks.

Example 1
Inputtasks = [1,2,1,2,3,1], space = 3
Output9
One way to complete all tasks in 9 days follows the required spacing between same-type tasks, and it can be shown that the tasks cannot be completed in less than 9 days.
Example 2
Inputtasks = [5,8,8,5], space = 2
Output6
One way to complete all tasks in 6 days follows the required spacing between same-type tasks, and it can be shown that the tasks cannot be completed in less than 6 days.

Constraints

  • 1 <= tasks.length <= 10^5
  • 1 <= tasks[i] <= 10^9
  • 1 <= space <= tasks.length

Asked at 6 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