#2365

Task Scheduler II

specialist · 805 · lc medium +31 · verified · 54.7% accepted · 619 likes · top 47%

Description

You are given a 0-indexed array of positive integers tasks, where tasks[i] is the type of the ith task to be completed in order.

A positive integer space specifies the minimum number of days that must elapse between two consecutive completions of the same task type.

Each day you must either complete the next task in line or take a rest day.

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

Example 1:

Input: tasks = [1,2,1,2,3,1], space = 3
Output: 9
Explanation:
One way to complete all tasks in 9 days is as follows:
Day 1: Complete the 0th task.
Day 2: Complete the 1st task.
Day 3: Take a break.
Day 4: Take a break.
Day 5: Complete the 2nd task.
Day 6: Complete the 3rd task.
Day 7: Take a break.
Day 8: Complete the 4th task.
Day 9: Complete the 5th task.
It can be shown that the tasks cannot be completed in less than 9 days.

Example 2:

Input: tasks = [5,8,8,5], space = 2
Output: 6
Explanation:
One way to complete all tasks in 6 days is as follows:
Day 1: Complete the 0th task.
Day 2: Complete the 1st task.
Day 3: Take a break.
Day 4: Take a break.
Day 5: Complete the 2nd task.
Day 6: Complete the 3rd task.
It can be shown that the tasks cannot be completed in less than 6 days.

Code

1
2
3