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.

&nbsp;
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.


&nbsp;
Constraints:


	1 &lt;= tasks.length &lt;= 105
	1 &lt;= tasks[i] &lt;= 109
	1 &lt;= space &lt;= tasks.length


import math
class Solution:
    def taskSchedulerII(self, tasks: List[int], space: int) -> int:
        count_dict = {}
        total_days = 0
        for task in tasks:
            if task not in count_dict:
                count_dict[task] = -math.inf
            total_days = max(total_days + 1, count_dict[task] + space + 1)
            count_dict[task] = total_days
        return total_days
class Solution {
    public long taskSchedulerII(int[] tasks, int space) {
        HashMap<Integer, Long> map = new HashMap<>();
        long day = 0;

        for (int item : tasks) {
            if (map.containsKey(item) && map.get(item) > day)
                day = map.get(item);

            day++;
            map.put(item, day + space);
        }

        return day;
    }
}
class Solution {
public:
    long long taskSchedulerII(vector<int>& tasks, int space) {
        unordered_map<int,long long int> hash; long long int curday=0;
        for(int i=0;i<tasks.size();i++){
            if(hash.count(tasks[i])) curday=max(curday,hash[tasks[i]]);
            hash[tasks[i]]=curday+space+1;
            curday+=1;
        }
        return curday;
    }
};
var taskSchedulerII = function(tasks, n) {
	const config = {};
	let totalIteration = 0;
	let currentTime = 0;
	for (const iterator of tasks) {
		currentTime++;
		if (!config[iterator]) {
			config[iterator] = 0;
		} else {
			if (config[iterator] > currentTime) {
				let difference = config[iterator] - currentTime;
				totalIteration += difference;
				currentTime += difference;
			}
		}
		config[iterator] = currentTime + n + 1;
		totalIteration++;
	}

	return totalIteration;
};
Task Scheduler II