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 an integer array nums of length n and a 2D array queries, where queries[i] = [li, ri, vali].
Each queries[i] represents the following action on nums:
[li, ri] from nums.vali.A Zero Array is an array with all its elements equal to 0.
Return the minimum possible non-negative value of k, such that after processing the first k queries in sequence, nums becomes a Zero Array. If no such k exists, return -1.
Example 1:
Input: nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]]
Output: 2
Explanation:
[0, 2] by 1.[1, 0, 1].[0, 2] by 1.[0, 0, 0], which is a Zero Array. Therefore, the minimum value of k is 2.Example 2:
Input: nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]]
Output: -1
Explanation:
It is impossible to make nums a Zero Array even after all the queries.
Example 3:
Input: nums = [1,2,3,2,1], queries = [[0,1,1],[1,2,1],[2,3,2],[3,4,1],[4,4,1]]
Output: 4
Explanation:
[0, 1] by 1.[0, 1, 3, 2, 1].[1, 2] by 1.[0, 0, 2, 2, 1].[2, 3] by 2.[0, 0, 0, 0, 1].[0, 0, 0, 0, 0]. Therefore, the minimum value of k is 4.Example 4:
Input: nums = [1,2,3,2,6], queries = [[0,1,1],[0,2,1],[1,4,2],[4,4,4],[3,4,1],[4,4,5]]
Output: 4
Constraints:
1 <= nums.length <= 100 <= nums[i] <= 10001 <= queries.length <= 1000queries[i] = [li, ri, vali]0 <= li <= ri < nums.length1 <= vali <= 10Problem summary: You are given an integer array nums of length n and a 2D array queries, where queries[i] = [li, ri, vali]. Each queries[i] represents the following action on nums: Select a subset of indices in the range [li, ri] from nums. Decrement the value at each selected index by exactly vali. A Zero Array is an array with all its elements equal to 0. Return the minimum possible non-negative value of k, such that after processing the first k queries in sequence, nums becomes a Zero Array. If no such k exists, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[2,0,2] [[0,2,1],[0,2,1],[1,1,3]]
[4,3,2,1] [[1,3,2],[0,2,1]]
[1,2,3,2,1] [[0,1,1],[1,2,1],[2,3,2],[3,4,1],[4,4,1]]
zero-array-transformation-i)zero-array-transformation-ii)zero-array-transformation-iii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3489: Zero Array Transformation IV
class Solution {
public int minZeroArray(int[] nums, int[][] queries) {
if (Arrays.stream(nums).allMatch(num -> num == 0))
return 0;
final int n = nums.length;
Set<Integer>[] subsetSums = new Set[n];
for (int i = 0; i < n; ++i)
subsetSums[i] = new HashSet<>(List.of(0));
for (int k = 0; k < queries.length; ++k) {
final int l = queries[k][0];
final int r = queries[k][1];
final int val = queries[k][2];
for (int i = l; i <= r; ++i) {
List<Integer> sums = new ArrayList<>();
for (final int subsetSum : subsetSums[i])
sums.add(subsetSum + val);
subsetSums[i].addAll(sums);
}
if (IntStream.range(0, n).allMatch(i -> subsetSums[i].contains(nums[i])))
return k + 1;
}
return -1;
}
}
// Accepted solution for LeetCode #3489: Zero Array Transformation IV
package main
import (
"math/big"
"slices"
"sort"
)
// https://space.bilibili.com/206214
func minZeroArray4(nums []int, queries [][]int) int {
ans := (slices.Max(nums) + 9) / 10
m := len(queries)
cnts := make([][11]int, m+1)
for i, x := range nums {
if x == 0 {
continue
}
for k, q := range queries {
cnts[k+1] = cnts[k]
if q[0] <= i && i <= q[1] {
cnts[k+1][q[2]]++
}
}
ans += sort.Search(m+1-ans, func(mx int) bool {
mx += ans
p := new(big.Int)
f := big.NewInt(1)
for v, num := range cnts[mx] {
for pow2 := 1; num > 0; pow2 *= 2 {
k := min(pow2, num)
f.Or(f, p.Lsh(f, uint(v*k)))
if f.Bit(x) > 0 {
return true
}
num -= k
}
}
return false
})
if ans > m {
return -1
}
}
return ans
}
func minZeroArray32(nums []int, queries [][]int) int {
m := len(queries)
cnts := make([][][11]int, m+1)
cnts[0] = make([][11]int, len(nums))
for k, q := range queries {
cnts[k+1] = slices.Clone(cnts[k])
for i := q[0]; i <= q[1]; i++ {
cnts[k+1][i][q[2]]++
}
}
ans := sort.Search(m+1, func(mx int) bool {
p := new(big.Int)
next:
for i, x := range nums {
if x == 0 {
continue
}
// 多重背包(二进制优化)
f := big.NewInt(1)
for v, num := range cnts[mx][i] {
for pow2 := 1; num > 0; pow2 *= 2 {
k := min(pow2, num)
f.Or(f, p.Lsh(f, uint(v*k)))
if f.Bit(x) > 0 {
continue next
}
num -= k
}
}
return false
}
return true
})
if ans <= m {
return ans
}
return -1
}
func minZeroArray3(nums []int, queries [][]int) int {
ans := sort.Search(len(queries)+1, func(mx int) bool {
p := new(big.Int)
next:
for i, x := range nums {
if x == 0 {
continue
}
cnt := [11]int{}
for _, q := range queries[:mx] {
if q[0] <= i && i <= q[1] {
cnt[q[2]]++
}
}
// 多重背包(二进制优化)
f := big.NewInt(1)
for v, num := range cnt {
for pow2 := 1; num > 0; pow2 *= 2 {
k := min(pow2, num)
f.Or(f, p.Lsh(f, uint(v*k)))
if f.Bit(x) > 0 {
continue next
}
num -= k
}
}
return false
}
return true
})
if ans <= len(queries) {
return ans
}
return -1
}
func minZeroArray2(nums []int, queries [][]int) (ans int) {
p := new(big.Int)
for i, x := range nums {
if x == 0 {
continue
}
f := big.NewInt(1)
for k, q := range queries {
if i < q[0] || i > q[1] {
continue
}
f.Or(f, p.Lsh(f, uint(q[2])))
if f.Bit(x) > 0 {
ans = max(ans, k+1)
break
}
}
if f.Bit(x) == 0 {
return -1
}
}
return
}
func minZeroArray(nums []int, queries [][]int) (ans int) {
for i, x := range nums {
if x == 0 {
continue
}
f := make([]bool, x+1)
f[0] = true
for k, q := range queries {
if i < q[0] || i > q[1] {
continue
}
val := q[2]
for j := x; j >= val; j-- {
f[j] = f[j] || f[j-val]
}
if f[x] {
ans = max(ans, k+1)
break
}
}
if !f[x] {
return -1
}
}
return
}
# Accepted solution for LeetCode #3489: Zero Array Transformation IV
class Solution:
def minZeroArray(self, nums: list[int], queries: list[list[int]]) -> int:
if all(num == 0 for num in nums):
return 0
n = len(nums)
subsetSums = [{0} for _ in range(n)]
for k, (l, r, val) in enumerate(queries):
for i in range(l, r + 1):
newSums = {subsetSum + val for subsetSum in subsetSums[i]}
subsetSums[i].update(newSums)
if all(nums[i] in subsetSums[i] for i in range(n)):
return k + 1
return -1
// Accepted solution for LeetCode #3489: Zero Array Transformation IV
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3489: Zero Array Transformation IV
// class Solution {
// public int minZeroArray(int[] nums, int[][] queries) {
// if (Arrays.stream(nums).allMatch(num -> num == 0))
// return 0;
//
// final int n = nums.length;
// Set<Integer>[] subsetSums = new Set[n];
//
// for (int i = 0; i < n; ++i)
// subsetSums[i] = new HashSet<>(List.of(0));
//
// for (int k = 0; k < queries.length; ++k) {
// final int l = queries[k][0];
// final int r = queries[k][1];
// final int val = queries[k][2];
// for (int i = l; i <= r; ++i) {
// List<Integer> sums = new ArrayList<>();
// for (final int subsetSum : subsetSums[i])
// sums.add(subsetSum + val);
// subsetSums[i].addAll(sums);
// }
// if (IntStream.range(0, n).allMatch(i -> subsetSums[i].contains(nums[i])))
// return k + 1;
// }
//
// return -1;
// }
// }
// Accepted solution for LeetCode #3489: Zero Array Transformation IV
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3489: Zero Array Transformation IV
// class Solution {
// public int minZeroArray(int[] nums, int[][] queries) {
// if (Arrays.stream(nums).allMatch(num -> num == 0))
// return 0;
//
// final int n = nums.length;
// Set<Integer>[] subsetSums = new Set[n];
//
// for (int i = 0; i < n; ++i)
// subsetSums[i] = new HashSet<>(List.of(0));
//
// for (int k = 0; k < queries.length; ++k) {
// final int l = queries[k][0];
// final int r = queries[k][1];
// final int val = queries[k][2];
// for (int i = l; i <= r; ++i) {
// List<Integer> sums = new ArrayList<>();
// for (final int subsetSum : subsetSums[i])
// sums.add(subsetSum + val);
// subsetSums[i].addAll(sums);
// }
// if (IntStream.range(0, n).allMatch(i -> subsetSums[i].contains(nums[i])))
// return k + 1;
// }
//
// return -1;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
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: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.