You're managing a project workflow where tasks must be completed in a specific order, but there's a cooldown period between tasks of the same type.
Given an array tasks where tasks[i] represents the type of the i-th task that must be completed, and an integer space representing the minimum number of days that must pass after completing a task before another task of the same type can be performed.
Each day you can either:
- Complete the next task from the tasks array (if allowed)
- Take a break (if the next task type is still in cooldown)
Goal: Return the minimum number of days needed to complete all tasks while respecting the cooldown constraints.
Example: tasks = [1,2,1,2,3,1], space = 3
Day 1: Complete task 1
Day 2: Complete task 2
Day 3: Break (task 1 still in cooldown)
Day 4: Break (task 1 still in cooldown)
Day 5: Break (task 1 still in cooldown)
Day 6: Complete task 1
...and so on
Input & Output
Visualization
Time & Space Complexity
We iterate through each task exactly once, with O(1) hash table operations
Where k is the number of unique task types (at most n)
Constraints
- 1 โค tasks.length โค 105
- 1 โค tasks[i] โค 109
- 0 โค space โค tasks.length
- Tasks must be completed in the given order