> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/hash-and-map/2365.-task-scheduler-ii.md).

# 2365. Task Scheduler II

## Problem Statement

<br>

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

&#x20;

**Example 1:**

<pre><code><strong>Input: tasks = [1,2,1,2,3,1], space = 3
</strong><strong>Output: 9
</strong><strong>Explanation:
</strong>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.
</code></pre>

**Example 2:**

<pre><code><strong>Input: tasks = [5,8,8,5], space = 2
</strong><strong>Output: 6
</strong><strong>Explanation:
</strong>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></pre>

&#x20;

**Constraints:**

* `1 <= tasks.length <= 105`
* `1 <= tasks[i] <= 109`
* `1 <= space <= tasks.length`

## Intuition

```
Approach:

Maintain a map to keep track of the next time, When it can be execute
```

### Links

<https://leetcode.com/problems/task-scheduler-ii/description/>

### Video Links

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    long long taskSchedulerII(vector<int>& tasks, int space) {
        unordered_map<int, long long> mp;
        long long cur_time=0;

        for(auto &it: tasks){
            if(mp.find(it) == mp.end()){
                mp[it] = cur_time+space+1;
                cur_time++;
            }
            else{
                // For taking the idle time, current time is less so have to wait
                if(cur_time < mp[it]){
                    cur_time = mp[it];
                }

                // No ideal time required, Directly shift the time ahead
                mp[it] = cur_time+space+1;
                cur_time++;
            }
        }

        return cur_time;
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###
