All problems
0948MediumArrayPrefix Sum

Can Every Reading Be Cleared

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3355Zero Array Transformation I

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def is_zero_array(nums: list[int], queries: list[list[int]]) -> bool:
Java
public boolean isZeroArray(int[] nums, int[][] queries)
September 7
Apply