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.
Given a sorted integer array nums and an integer n, add/patch elements to the array such that any number in the range [1, n] inclusive can be formed by the sum of some elements in the array.
Return the minimum number of patches required.
Example 1:
Input: nums = [1,3], n = 6 Output: 1 Explanation: Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4. Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3]. Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6]. So we only need 1 patch.
Example 2:
Input: nums = [1,5,10], n = 20 Output: 2 Explanation: The two patches can be [2, 4].
Example 3:
Input: nums = [1,2,2], n = 5 Output: 0
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 104nums is sorted in ascending order.1 <= n <= 231 - 1Problem summary: Given a sorted integer array nums and an integer n, add/patch elements to the array such that any number in the range [1, n] inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,3] 6
[1,5,10] 20
[1,2,2] 5
maximum-number-of-consecutive-values-you-can-make)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #330: Patching Array
class Solution {
public int minPatches(int[] nums, int n) {
long x = 1;
int ans = 0;
for (int i = 0; x <= n;) {
if (i < nums.length && nums[i] <= x) {
x += nums[i++];
} else {
++ans;
x <<= 1;
}
}
return ans;
}
}
// Accepted solution for LeetCode #330: Patching Array
func minPatches(nums []int, n int) (ans int) {
x := 1
for i := 0; x <= n; {
if i < len(nums) && nums[i] <= x {
x += nums[i]
i++
} else {
ans++
x <<= 1
}
}
return
}
# Accepted solution for LeetCode #330: Patching Array
class Solution:
def minPatches(self, nums: List[int], n: int) -> int:
x = 1
ans = i = 0
while x <= n:
if i < len(nums) and nums[i] <= x:
x += nums[i]
i += 1
else:
ans += 1
x <<= 1
return ans
// Accepted solution for LeetCode #330: Patching Array
struct Solution;
impl Solution {
fn min_patches(nums: Vec<i32>, n: i32) -> i32 {
let mut res = 0;
let mut miss: i64 = 1;
let mut i = 0;
while miss <= n as i64 {
if i < nums.len() && nums[i] as i64 <= miss {
miss += nums[i] as i64;
i += 1;
} else {
miss += miss;
res += 1;
}
}
res
}
}
#[test]
fn test() {
let nums = vec![1, 3];
let n = 6;
let res = 1;
assert_eq!(Solution::min_patches(nums, n), res);
let nums = vec![1, 5, 10];
let n = 20;
let res = 2;
assert_eq!(Solution::min_patches(nums, n), res);
let nums = vec![1, 2, 2];
let n = 5;
let res = 0;
assert_eq!(Solution::min_patches(nums, n), res);
}
// Accepted solution for LeetCode #330: Patching Array
function minPatches(nums: number[], n: number): number {
let x = 1;
let ans = 0;
for (let i = 0; x <= n; ) {
if (i < nums.length && nums[i] <= x) {
x += nums[i++];
} else {
++ans;
x *= 2;
}
}
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.