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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
There is a computer that can run an unlimited number of tasks at the same time. You are given a 2D integer array tasks where tasks[i] = [starti, endi, durationi] indicates that the ith task should run for a total of durationi seconds (not necessarily continuous) within the inclusive time range [starti, endi].
You may turn on the computer only when it needs to run a task. You can also turn it off if it is idle.
Return the minimum time during which the computer should be turned on to complete all tasks.
Example 1:
Input: tasks = [[2,3,1],[4,5,1],[1,5,2]] Output: 2 Explanation: - The first task can be run in the inclusive time range [2, 2]. - The second task can be run in the inclusive time range [5, 5]. - The third task can be run in the two inclusive time ranges [2, 2] and [5, 5]. The computer will be on for a total of 2 seconds.
Example 2:
Input: tasks = [[1,3,2],[2,5,3],[5,6,2]] Output: 4 Explanation: - The first task can be run in the inclusive time range [2, 3]. - The second task can be run in the inclusive time ranges [2, 3] and [5, 5]. - The third task can be run in the two inclusive time range [5, 6]. The computer will be on for a total of 4 seconds.
Constraints:
1 <= tasks.length <= 2000tasks[i].length == 31 <= starti, endi <= 20001 <= durationi <= endi - starti + 1 Problem summary: There is a computer that can run an unlimited number of tasks at the same time. You are given a 2D integer array tasks where tasks[i] = [starti, endi, durationi] indicates that the ith task should run for a total of durationi seconds (not necessarily continuous) within the inclusive time range [starti, endi]. You may turn on the computer only when it needs to run a task. You can also turn it off if it is idle. Return the minimum time during which the computer should be turned on to complete all tasks.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Stack · Greedy
[[2,3,1],[4,5,1],[1,5,2]]
[[1,3,2],[2,5,3],[5,6,2]]
single-threaded-cpu)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2589: Minimum Time to Complete All Tasks
class Solution {
public int findMinimumTime(int[][] tasks) {
Arrays.sort(tasks, (a, b) -> a[1] - b[1]);
int[] vis = new int[2010];
int ans = 0;
for (var task : tasks) {
int start = task[0], end = task[1], duration = task[2];
for (int i = start; i <= end; ++i) {
duration -= vis[i];
}
for (int i = end; i >= start && duration > 0; --i) {
if (vis[i] == 0) {
--duration;
ans += vis[i] = 1;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2589: Minimum Time to Complete All Tasks
func findMinimumTime(tasks [][]int) (ans int) {
sort.Slice(tasks, func(i, j int) bool { return tasks[i][1] < tasks[j][1] })
vis := [2010]int{}
for _, task := range tasks {
start, end, duration := task[0], task[1], task[2]
for _, x := range vis[start : end+1] {
duration -= x
}
for i := end; i >= start && duration > 0; i-- {
if vis[i] == 0 {
vis[i] = 1
duration--
ans++
}
}
}
return
}
# Accepted solution for LeetCode #2589: Minimum Time to Complete All Tasks
class Solution:
def findMinimumTime(self, tasks: List[List[int]]) -> int:
tasks.sort(key=lambda x: x[1])
vis = [0] * 2010
ans = 0
for start, end, duration in tasks:
duration -= sum(vis[start : end + 1])
i = end
while i >= start and duration > 0:
if not vis[i]:
duration -= 1
vis[i] = 1
ans += 1
i -= 1
return ans
// Accepted solution for LeetCode #2589: Minimum Time to Complete All Tasks
impl Solution {
pub fn find_minimum_time(tasks: Vec<Vec<i32>>) -> i32 {
let mut tasks = tasks;
tasks.sort_by(|a, b| a[1].cmp(&b[1]));
let mut vis = vec![0; 2010];
let mut ans = 0;
for task in tasks {
let start = task[0] as usize;
let end = task[1] as usize;
let mut duration = task[2] - vis[start..=end].iter().sum::<i32>();
let mut i = end;
while i >= start && duration > 0 {
if vis[i] == 0 {
duration -= 1;
vis[i] = 1;
ans += 1;
}
i -= 1;
}
}
ans
}
}
// Accepted solution for LeetCode #2589: Minimum Time to Complete All Tasks
function findMinimumTime(tasks: number[][]): number {
tasks.sort((a, b) => a[1] - b[1]);
const vis: number[] = Array(2010).fill(0);
let ans = 0;
for (let [start, end, duration] of tasks) {
for (let i = start; i <= end; ++i) {
duration -= vis[i];
}
for (let i = end; i >= start && duration > 0; --i) {
if (vis[i] === 0) {
--duration;
ans += vis[i] = 1;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
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: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.