Skip to content

Practice Zero Array Transformation IV

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3489
LeetCode Problem

Zero Array Transformation IV

Problem Statement

Given nums and ordered queries [left, right, value], each query lets you choose any subset of indices in its inclusive range and subtract exactly value from each chosen element. Return the smallest nonnegative k such that some choices for the first k queries make every element zero. Return -1 if this is impossible.

Source: Zero Array Transformation IV on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]]
Output
2

# Example 2

Input
nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]]
Output
-1

# 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

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

Related LeetCode Problems

Practice more problems similar to Zero Array Transformation IV.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.