1+ / *
2+
3+ 3356. Zero Array Transformation II
4+
5+ You are given an integer array nums of length n and a 2 D array queries where queries [i ] = [li , ri , vali ].
6+
7+ Each queries [i ] represents the following action on nums :
8+
9+ Decrement the value at each index in the range [li , ri ] in nums by at most vali .
10+ The amount by which each value is decremented can be chosen independently for each index .
11+ A Zero Array is an array with all its elements equal to 0.
12+
13+ 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.
14+
15+
16+
17+ Example 1 :
18+
19+ Input : nums = [2 ,0 ,2 ], queries = [[0 ,2 ,1 ],[0 ,2 ,1 ],[1 ,1 ,3 ]]
20+
21+ Output : 2
22+
23+ Explanation :
24+
25+ For i = 0 (l = 0 , r = 2 , val = 1 ):
26+ Decrement values at indices [0 , 1 , 2 ] by [1 , 0 , 1 ] respectively .
27+ The array will become [1 , 0 , 1 ].
28+ For i = 1 (l = 0 , r = 2 , val = 1 ):
29+ Decrement values at indices [0 , 1 , 2 ] by [1 , 0 , 1 ] respectively .
30+ The array will become [0 , 0 , 0 ], which is a Zero Array . Therefore , the minimum value of k is 2.
31+ Example 2 :
32+
33+ Input : nums = [4 ,3 ,2 ,1 ], queries = [[1 ,3 ,2 ],[0 ,2 ,1 ]]
34+
35+ Output : - 1
36+
37+ Explanation :
38+
39+ For i = 0 (l = 1 , r = 3 , val = 2 ):
40+ Decrement values at indices [1 , 2 , 3 ] by [2 , 2 , 1 ] respectively .
41+ The array will become [4 , 1 , 0 , 0 ].
42+ For i = 1 (l = 0 , r = 2 , val = 1 ):
43+ Decrement values at indices [0 , 1 , 2 ] by [1 , 1 , 0 ] respectively .
44+ The array will become [3 , 0 , 0 , 0 ], which is not a Zero Array .
45+
46+
47+ Constraints :
48+
49+ 1 <= nums .length <= 105
50+ 0 <= nums [i ] <= 5 * 105
51+ 1 <= queries .length <= 105
52+ queries [i ].length == 3
53+ 0 <= li <= ri < nums .length
54+ 1 <= vali <= 5
55+
56+ * /
57+
58+ class Solution :
59+ def minZeroArray (self , nums : List [int ], queries : List [List [int ]]) -> int :
60+ def check (k : int ) -> bool :
61+ d = [0 ] * (len (nums ) + 1 )
62+ for l , r , val in queries [:k ]:
63+ d [l ] += val
64+ d [r + 1 ] -= val
65+ s = 0
66+ for x , y in zip (nums , d ):
67+ s += y
68+ if x > s :
69+ return False
70+ return True
71+
72+ m = len (queries )
73+ l = bisect_left (range (m + 1 ), True , key = check )
74+ return - 1 if l > m else l
0 commit comments