<p>You are given a <strong>0-indexed</strong> array of positive integers <code>tasks</code>, representing tasks that need to be completed <strong>in order</strong>, where <code>tasks[i]</code> represents the <strong>type</strong> of the <code>i<sup>th</sup></code> task.</p>

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

<p>Each day, until all tasks have been completed, you must either:</p>

<ul>
	<li>Complete the next task from <code>tasks</code>, or</li>
	<li>Take a break.</li>
</ul>

<p>Return<em> the <strong>minimum</strong> number of days needed to complete all tasks</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= tasks.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= tasks[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= space &lt;= tasks.length</code></li>
</ul>

class Solution:
    def taskSchedulerII(self, tasks: List[int], space: int) -> int:

        last_day = {}  # Dictionary to keep track of the last day a task was performed
        current_day = 1  # Start at day 1
        
        for task in tasks:
            if task in last_day:
                #print(task)
                # Calculate the next possible day you can perform this task
                next_available_day = last_day[task] + space + 1
                # Perform the task on the later of the current day or the next available day
                current_day = max(current_day, next_available_day)
                print(f'current day {current_day}')
                #last_day[task] = current_day
                #current_day += 1
           
            last_day[task] = current_day
            current_day += 1
                #print(current_day)
            # Update the last day this task was performed
        
        
        return current_day - 1
"""
# Example usage
tasks1 = [1, 2, 1, 2, 3, 1]
space1 = 3
print(minDaysToCompleteTasks(tasks1, space1))  # Output: 9

tasks2 = [5, 8, 8, 5]
space2 = 2
print(minDaysToCompleteTasks(tasks2, space2))  # Output: 6


tasks2 = [5, 8, 8, 5]
space2 = 2
print(minDaysToCompleteTasks(tasks2, space2))  # Output: 6
"""
34300