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.
You are given an array tasks where tasks[i] = [actuali, minimumi]:
actuali is the actual amount of energy you spend to finish the ith task.minimumi is the minimum amount of energy you require to begin the ith task.For example, if the task is [10, 12] and your current energy is 11, you cannot start this task. However, if your current energy is 13, you can complete this task, and your energy will be 3 after finishing it.
You can finish the tasks in any order you like.
Return the minimum initial amount of energy you will need to finish all the tasks.
Example 1:
Input: tasks = [[1,2],[2,4],[4,8]]
Output: 8
Explanation:
Starting with 8 energy, we finish the tasks in the following order:
- 3rd task. Now energy = 8 - 4 = 4.
- 2nd task. Now energy = 4 - 2 = 2.
- 1st task. Now energy = 2 - 1 = 1.
Notice that even though we have leftover energy, starting with 7 energy does not work because we cannot do the 3rd task.
Example 2:
Input: tasks = [[1,3],[2,4],[10,11],[10,12],[8,9]]
Output: 32
Explanation:
Starting with 32 energy, we finish the tasks in the following order:
- 1st task. Now energy = 32 - 1 = 31.
- 2nd task. Now energy = 31 - 2 = 29.
- 3rd task. Now energy = 29 - 10 = 19.
- 4th task. Now energy = 19 - 10 = 9.
- 5th task. Now energy = 9 - 8 = 1.
Example 3:
Input: tasks = [[1,7],[2,8],[3,9],[4,10],[5,11],[6,12]]
Output: 27
Explanation:
Starting with 27 energy, we finish the tasks in the following order:
- 5th task. Now energy = 27 - 5 = 22.
- 2nd task. Now energy = 22 - 2 = 20.
- 3rd task. Now energy = 20 - 3 = 17.
- 1st task. Now energy = 17 - 1 = 16.
- 4th task. Now energy = 16 - 4 = 12.
- 6th task. Now energy = 12 - 6 = 6.
Constraints:
1 <= tasks.length <= 1051 <= actuali <= minimumi <= 104Problem summary: You are given an array tasks where tasks[i] = [actuali, minimumi]: actuali is the actual amount of energy you spend to finish the ith task. minimumi is the minimum amount of energy you require to begin the ith task. For example, if the task is [10, 12] and your current energy is 11, you cannot start this task. However, if your current energy is 13, you can complete this task, and your energy will be 3 after finishing it. You can finish the tasks in any order you like. Return the minimum initial amount of energy you will need to finish all the tasks.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[[1,2],[2,4],[4,8]]
[[1,3],[2,4],[10,11],[10,12],[8,9]]
[[1,7],[2,8],[3,9],[4,10],[5,11],[6,12]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1665: Minimum Initial Energy to Finish Tasks
class Solution {
public int minimumEffort(int[][] tasks) {
Arrays.sort(tasks, (a, b) -> a[0] - b[0] - (a[1] - b[1]));
int ans = 0, cur = 0;
for (var task : tasks) {
int a = task[0], m = task[1];
if (cur < m) {
ans += m - cur;
cur = m;
}
cur -= a;
}
return ans;
}
}
// Accepted solution for LeetCode #1665: Minimum Initial Energy to Finish Tasks
func minimumEffort(tasks [][]int) (ans int) {
sort.Slice(tasks, func(i, j int) bool { return tasks[i][0]-tasks[i][1] < tasks[j][0]-tasks[j][1] })
cur := 0
for _, task := range tasks {
a, m := task[0], task[1]
if cur < m {
ans += m - cur
cur = m
}
cur -= a
}
return
}
# Accepted solution for LeetCode #1665: Minimum Initial Energy to Finish Tasks
class Solution:
def minimumEffort(self, tasks: List[List[int]]) -> int:
ans = cur = 0
for a, m in sorted(tasks, key=lambda x: x[0] - x[1]):
if cur < m:
ans += m - cur
cur = m
cur -= a
return ans
// Accepted solution for LeetCode #1665: Minimum Initial Energy to Finish Tasks
struct Solution;
impl Solution {
fn minimum_effort(tasks: Vec<Vec<i32>>) -> i32 {
let n = tasks.len();
let mut sum = 0;
let mut min = std::i32::MAX;
let mut max = 0;
for i in 0..n {
sum += tasks[i][0];
min = min.min(tasks[i][1] - tasks[i][0]);
max = max.max(tasks[i][1]);
}
(sum + min).max(max)
}
}
#[test]
fn test() {
let tasks = vec_vec_i32![[1, 2], [2, 4], [4, 8]];
let res = 8;
assert_eq!(Solution::minimum_effort(tasks), res);
let tasks = vec_vec_i32![[1, 3], [2, 4], [10, 11], [10, 12], [8, 9]];
let res = 32;
assert_eq!(Solution::minimum_effort(tasks), res);
let tasks = vec_vec_i32![[1, 7], [2, 8], [3, 9], [4, 10], [5, 11], [6, 12]];
let res = 27;
assert_eq!(Solution::minimum_effort(tasks), res);
let tasks = vec_vec_i32![[1, 1], [1, 3]];
let res = 3;
assert_eq!(Solution::minimum_effort(tasks), res);
}
// Accepted solution for LeetCode #1665: Minimum Initial Energy to Finish Tasks
function minimumEffort(tasks: number[][]): number {
tasks.sort((a, b) => a[0] - a[1] - (b[0] - b[1]));
let ans = 0;
let cur = 0;
for (const [a, m] of tasks) {
if (cur < m) {
ans += m - cur;
cur = m;
}
cur -= a;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
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: 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.