Zero Array Transformation II
You are given an integer array nums of length n and an array queries, where queries[j] = [l_j, r_j, val_j].
Process queries in order. When you apply a query [l, r, val], for every index i in the inclusive range [l, r] you may independently decrement nums[i] by any integer amount in [0, val] (possibly different amounts at different indices, and possibly 0).
Return the minimum number of queries k (a prefix length, so you use the first k queries in order) such that it is possible to make every element of nums equal to 0. If nums is already all zeros, return 0. If it is impossible even after using all queries, return -1.
Examples
Input: [[2,0,2],[[0,2,1],[0,2,1],[1,1,3]]]
Output: 2
Input: [[4,3,2,1],[[1,3,2],[0,2,1]]]
Output: -1
Hints
For a fixed prefix of `k` queries, index `i` can be reduced by at most the sum of `val_j` over queries `j < k` covering `i`; feasibility means this capacity is at least `nums[i]` for all `i`.
If the first `k` queries are feasible, any larger prefix is also feasible, so you can binary-search the minimum `k`.
To test a prefix quickly, apply range additions with a difference array and sweep prefix sums left to right.
Related Problems
Zero Array Transformation II
You are given an integer array `nums` of length `n` and an array `queries`, where `queries[j] = [l_j, r_j, val_j]`.