3489. Zero Array Transformation IV Medium

@problem@discussion
#Array#Dynamic Programming



1/**
2 * [3489] Zero Array Transformation IV
3 *
4 * You are given an integer array nums of length n and a 2D array queries, where queries[i] = [li, ri, vali].
5 * Each queries[i] represents the following action on nums:
6 * 
7 * 	Select a <span data-keyword="subset">subset</span> of indices in the range [li, ri] from nums.
8 * 	Decrement the value at each selected index by exactly vali.
9 * 
10 * A Zero Array is an array with all its elements equal to 0.
11 * 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.
12 *  
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]]</span>
16 * Output: <span class="example-io">2</span>
17 * Explanation:
18 * 
19 * 	For query 0 (l = 0, r = 2, val = 1):
20 * 	
21 * 		Decrement the values at indices [0, 2] by 1.
22 * 		The array will become [1, 0, 1].
23 * 	
24 * 	
25 * 	For query 1 (l = 0, r = 2, val = 1):
26 * 	
27 * 		Decrement the values at indices [0, 2] by 1.
28 * 		The array will become [0, 0, 0], which is a Zero Array. Therefore, the minimum value of k is 2.
29 * 	
30 * 	
31 * </div>
32 * <strong class="example">Example 2:
33 * <div class="example-block">
34 * Input: <span class="example-io">nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]]</span>
35 * Output: <span class="example-io">-1</span>
36 * Explanation:
37 * It is impossible to make nums a Zero Array even after all the queries.
38 * </div>
39 * <strong class="example">Example 3:
40 * <div class="example-block">
41 * Input: <span class="example-io">nums = [1,2,3,2,1], queries = [[0,1,1],[1,2,1],[2,3,2],[3,4,1],[4,4,1]]</span>
42 * Output: <span class="example-io">4</span>
43 * Explanation:
44 * 
45 * 	For query 0 (l = 0, r = 1, val = 1):
46 * 	
47 * 		Decrement the values at indices [0, 1] by <font face="monospace">1</font>.
48 * 		The array will become [0, 1, 3, 2, 1].
49 * 	
50 * 	
51 * 	For query 1 (l = 1, r = 2, val = 1):
52 * 	
53 * 		Decrement the values at indices [1, 2] by 1.
54 * 		The array will become [0, 0, 2, 2, 1].
55 * 	
56 * 	
57 * 	For query 2 (l = 2, r = 3, val = 2):
58 * 	
59 * 		Decrement the values at indices [2, 3] by 2.
60 * 		The array will become [0, 0, 0, 0, 1].
61 * 	
62 * 	
63 * 	For query 3 (l = 3, r = 4, val = 1):
64 * 	
65 * 		Decrement the value at index 4 by 1.
66 * 		The array will become [0, 0, 0, 0, 0]. Therefore, the minimum value of k is 4.
67 * 	
68 * 	
69 * </div>
70 * <strong class="example">Example 4:
71 * <div class="example-block">
72 * Input: <span class="example-io">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]]</span>
73 * Output: <span class="example-io">4</span>
74 * </div>
75 *  
76 * Constraints:
77 * 
78 * 	1 <= nums.length <= 10
79 * 	0 <= nums[i] <= 1000
80 * 	1 <= queries.length <= 1000
81 * 	queries[i] = [li, ri, vali]
82 * 	0 <= li <= ri < nums.length
83 * 	1 <= vali <= 10
84 * 
85 */
86pub struct Solution {}
87
88// problem: https://leetcode.com/problems/zero-array-transformation-iv/
89// discuss: https://leetcode.com/problems/zero-array-transformation-iv/discuss/?currentPage=1&orderBy=most_votes&query=
90
91// submission codes start here
92
93impl Solution {
94    pub fn min_zero_array(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> i32 {
95        0
96    }
97}
98
99// submission codes end
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn test_3489() {
107    }
108}
109

Back
© 2026 bowen.ge All Rights Reserved.