Task Scheduler II - Problem
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-th 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.
Input & Output
Example 1 — Basic Case
$
Input:
tasks = [1,2,1,2], space = 3
›
Output:
6
💡 Note:
Day 1: Task 1, Day 2: Task 2, Day 5: Task 1 (wait 3 days), Day 6: Task 2 (wait 3 days)
Example 2 — No Waiting Needed
$
Input:
tasks = [5,8,8,5], space = 2
›
Output:
7
💡 Note:
Day 1: Task 5, Day 2: Task 8, Day 5: Task 8 (wait 2 days), Day 7: Task 5 (wait 2 days)
Example 3 — Single Task Type
$
Input:
tasks = [1,1,1], space = 2
›
Output:
7
💡 Note:
Day 1: Task 1, Day 4: Task 1 (wait 2 days), Day 7: Task 1 (wait 2 days)
Constraints
- 1 ≤ tasks.length ≤ 105
- 1 ≤ tasks[i] ≤ 109
- 0 ≤ space ≤ tasks.length
Visualization
Tap to expand
Understanding the Visualization
1
Input
Tasks [1,2,1,2] with space=3 cooldown
2
Process
Schedule tasks with mandatory waiting periods
3
Output
Minimum 6 days needed to complete all tasks
Key Takeaway
🎯 Key Insight: Use hash map to track last completion day for each task type, enabling O(1) cooldown validation
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code