Problem

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.

Example 1

1
2
3
4
5
6
7
8
9
Input: nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]]
Output: 2
Explanation:
* **For query 0 (l = 0, r = 2, val = 1):**
* Decrement the values at indices `[0, 2]` by 1.
* The array will become `[1, 0, 1]`.
* **For query 1 (l = 0, r = 2, val = 1):**
* Decrement the values at indices `[0, 2]` by 1.
* The array will become `[0, 0, 0]`, which is a Zero Array. Therefore, the minimum value of `k` is 2.

Example 2

1
2
3
4
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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:
* **For query 0 (l = 0, r = 1, val = 1):**
* Decrement the values at indices `[0, 1]` by `1`.
* The array will become `[0, 1, 3, 2, 1]`.
* **For query 1 (l = 1, r = 2, val = 1):**
* Decrement the values at indices `[1, 2]` by 1.
* The array will become `[0, 0, 2, 2, 1]`.
* **For query 2 (l = 2, r = 3, val = 2):**
* Decrement the values at indices `[2, 3]` by 2.
* The array will become `[0, 0, 0, 0, 1]`.
* **For query 3 (l = 3, r = 4, val = 1):**
* Decrement the value at index 4 by 1.
* The array will become `[0, 0, 0, 0, 0]`. Therefore, the minimum value of `k` is 4.

Example 4

1
2
3
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 <= 10
  • 0 <= nums[i] <= 1000
  • 1 <= queries.length <= 1000
  • queries[i] = [li, ri, vali]
  • 0 <= li <= ri < nums.length
  • 1 <= vali <= 10

Examples

Solution

Method 1 – Brute Force Simulation

Intuition

Since the array is small (n ≤ 10), we can simulate the process for each prefix of queries. For each k, apply the first k queries in order, always greedily decrementing as much as possible in the allowed range, and check if the array becomes all zeros.

Approach

  1. For k from 1 to len(queries):
    • Copy the original nums array.
    • For each query up to k:
      • For each index in the range [li, ri]:
        • If nums[idx] >= vali, decrement nums[idx] by vali.
    • After all k queries, check if all elements are zero.
    • If so, return k.
  2. If no such k exists, return -1.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    int minQueriesToZeroArray(vector<int>& nums, vector<vector<int>>& queries) {
        int n = nums.size(), q = queries.size();
        for (int k = 1; k <= q; ++k) {
            vector<int> arr = nums;
            for (int i = 0; i < k; ++i) {
                int l = queries[i][0], r = queries[i][1], v = queries[i][2];
                for (int j = l; j <= r; ++j) {
                    if (arr[j] >= v) arr[j] -= v;
                }
            }
            bool allZero = true;
            for (int x : arr) if (x != 0) allZero = false;
            if (allZero) return k;
        }
        return -1;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
func minQueriesToZeroArray(nums []int, queries [][]int) int {
    n, q := len(nums), len(queries)
    for k := 1; k <= q; k++ {
        arr := make([]int, n)
        copy(arr, nums)
        for i := 0; i < k; i++ {
            l, r, v := queries[i][0], queries[i][1], queries[i][2]
            for j := l; j <= r; j++ {
                if arr[j] >= v {
                    arr[j] -= v
                }
            }
        }
        allZero := true
        for _, x := range arr {
            if x != 0 {
                allZero = false
                break
            }
        }
        if allZero {
            return k
        }
    }
    return -1
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
    public int minQueriesToZeroArray(int[] nums, int[][] queries) {
        int n = nums.length, q = queries.length;
        for (int k = 1; k <= q; k++) {
            int[] arr = nums.clone();
            for (int i = 0; i < k; i++) {
                int l = queries[i][0], r = queries[i][1], v = queries[i][2];
                for (int j = l; j <= r; j++) {
                    if (arr[j] >= v) arr[j] -= v;
                }
            }
            boolean allZero = true;
            for (int x : arr) if (x != 0) allZero = false;
            if (allZero) return k;
        }
        return -1;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
    fun minQueriesToZeroArray(nums: IntArray, queries: Array<IntArray>): Int {
        val n = nums.size
        val q = queries.size
        for (k in 1..q) {
            val arr = nums.copyOf()
            for (i in 0 until k) {
                val (l, r, v) = queries[i]
                for (j in l..r) {
                    if (arr[j] >= v) arr[j] -= v
                }
            }
            if (arr.all { it == 0 }) return k
        }
        return -1
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from typing import List
class Solution:
    def minQueriesToZeroArray(self, nums: List[int], queries: List[List[int]]) -> int:
        n, q = len(nums), len(queries)
        for k in range(1, q+1):
            arr = nums[:]
            for i in range(k):
                l, r, v = queries[i]
                for j in range(l, r+1):
                    if arr[j] >= v:
                        arr[j] -= v
            if all(x == 0 for x in arr):
                return k
        return -1
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
impl Solution {
    pub fn min_queries_to_zero_array(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> i32 {
        let n = nums.len();
        let q = queries.len();
        for k in 1..=q {
            let mut arr = nums.clone();
            for i in 0..k {
                let l = queries[i][0] as usize;
                let r = queries[i][1] as usize;
                let v = queries[i][2];
                for j in l..=r {
                    if arr[j] >= v {
                        arr[j] -= v;
                    }
                }
            }
            if arr.iter().all(|&x| x == 0) {
                return k as i32;
            }
        }
        -1
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
    minQueriesToZeroArray(nums: number[], queries: number[][]): number {
        const n = nums.length, q = queries.length;
        for (let k = 1; k <= q; k++) {
            const arr = nums.slice();
            for (let i = 0; i < k; i++) {
                const [l, r, v] = queries[i];
                for (let j = l; j <= r; j++) {
                    if (arr[j] >= v) arr[j] -= v;
                }
            }
            if (arr.every(x => x === 0)) return k;
        }
        return -1;
    }
}

Complexity

  • ⏰ Time complexity: O(q * n^2) — For each prefix of queries (up to q), we may update up to n elements for each query, and there are up to q queries, so O(q^2 * n) in the worst case, but n is small (≤ 10).
  • 🧺 Space complexity: O(n) — We use a copy of the nums array for each prefix.