Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
tasks, orReturn the minimum number of days needed to complete all tasks.
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.
Constraints:
1 <= tasks.length <= 1051 <= tasks[i] <= 1091 <= space <= tasks.lengthProblem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,1,2,3,1] 3
[5,8,8,5] 2
task-scheduler)maximize-distance-to-closest-person)check-if-all-1s-are-at-least-length-k-places-away)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2365: Task Scheduler II
class Solution {
public long taskSchedulerII(int[] tasks, int space) {
Map<Integer, Long> day = new HashMap<>();
long ans = 0;
for (int task : tasks) {
++ans;
ans = Math.max(ans, day.getOrDefault(task, 0L));
day.put(task, ans + space + 1);
}
return ans;
}
}
// Accepted solution for LeetCode #2365: Task Scheduler II
func taskSchedulerII(tasks []int, space int) (ans int64) {
day := map[int]int64{}
for _, task := range tasks {
ans++
if ans < day[task] {
ans = day[task]
}
day[task] = ans + int64(space) + 1
}
return
}
# Accepted solution for LeetCode #2365: Task Scheduler II
class Solution:
def taskSchedulerII(self, tasks: List[int], space: int) -> int:
day = defaultdict(int)
ans = 0
for task in tasks:
ans += 1
ans = max(ans, day[task])
day[task] = ans + space + 1
return ans
// Accepted solution for LeetCode #2365: Task Scheduler II
/**
* [2365] Task Scheduler II
*
* 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.
*
* 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.
*
*
* Constraints:
*
* 1 <= tasks.length <= 10^5
* 1 <= tasks[i] <= 10^9
* 1 <= space <= tasks.length
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/task-scheduler-ii/
// discuss: https://leetcode.com/problems/task-scheduler-ii/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn task_scheduler_ii(tasks: Vec<i32>, space: i32) -> i64 {
let mut hm = std::collections::HashMap::new();
let mut result: i64 = 0;
for t in tasks {
if hm.contains_key(&t) {
let past = result - hm.get(&t).unwrap() - 1;
if past < space as i64 {
result += space as i64 - past;
}
}
hm.insert(t, result);
result += 1;
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2365_example_1() {
let tasks = vec![1, 2, 1, 2, 3, 1];
let space = 3;
let result = 9;
assert_eq!(Solution::task_scheduler_ii(tasks, space), result);
}
#[test]
fn test_2365_example_2() {
let tasks = vec![5, 8, 8, 5];
let space = 2;
let result = 6;
assert_eq!(Solution::task_scheduler_ii(tasks, space), result);
}
}
// Accepted solution for LeetCode #2365: Task Scheduler II
function taskSchedulerII(tasks: number[], space: number): number {
const day = new Map<number, number>();
let ans = 0;
for (const task of tasks) {
++ans;
ans = Math.max(ans, day.get(task) ?? 0);
day.set(task, ans + space + 1);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.