Zero Array Transformation I
Real-world version: Can Every Reading Be ClearedThe statement here is our own: the same inputs, outputs and technique.
Readings are given as nums, and each entry of queries is a pair [l, r].
Working through the queries in order, a query lets you pick any set of positions inside its own range, ends included, and drop each picked reading by one. Picking nothing is allowed, and a reading may never be dropped below zero.
Return true when every reading can be brought down to zero by the time all the queries have been worked through.
Examples
Example 1
- Input
- nums = [2, 0, 3, 1], queries = [[0, 3], [2, 2], [0, 2], [2, 3]]
- Output
- true
Position 0 is covered by two queries and reads 2, position 2 is covered by all four and reads 3, and position 3 is covered by two and reads 1, so every reading has enough cover.
Example 2
- Input
- nums = [1, 1, 1], queries = [[0, 2]]
- Output
- true
One query covers all three positions, so each can be dropped once, which is exactly what each reading needs.
Example 3
- Input
- nums = [2], queries = [[0, 0]]
- Output
- false
The single query can drop the reading only once, leaving it at one.
Constraints
- 1 <= nums.length <= 10^5
- 0 <= nums[i] <= 10^5
- 1 <= queries.length <= 10^5
- queries[i].length == 2
- 0 <= queries[i][0] <= queries[i][1] <= nums.length - 1
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def is_zero_array(nums: list[int], queries: list[list[int]]) -> bool:public boolean isZeroArray(int[] nums, int[][] queries)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's