**Physical Address**

304 North Cardinal St.

Dorchester Center, MA 02124

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`

task.^{th}

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:**

```
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.
```

**Constraints:**

`1 <= tasks.length <= 10`

^{5}`1 <= tasks[i] <= 10`

^{9}`1 <= space <= tasks.length`

```
public long taskSchedulerII(int[] tasks, int space) {
Map<Integer, Long> last = new HashMap<>();;
long res = 0;
for (int a : tasks)
if (last.containsKey(a))
last.put(a, res = Math.max(res, last.get(a) + space) + 1);
else
last.put(a, ++res);
return res;
}
```

```
long long taskSchedulerII(vector<int>& tasks, int space) {
unordered_map<int, long long> last;
long long res = 0;
for (int a: tasks)
if (last.count(a))
last[a] = res = max(res, last[a] + space) + 1;
else
last[a] = ++res;
return res;
}
```

```
def taskSchedulerII(self, A, space):
last = defaultdict(lambda: - len(A) - 10)
res = 0
for a in A:
last[a] = res = max(res, last[a] + space) + 1
return res
```

In our experience, we suggest you solve this Task Scheduler II LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Task Scheduler II LeetCode Solution

I hope this Task Scheduler II LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

**More Coding Solutions >>**