You are given an integer array nums of length n and a 2D array queries, where queries[i] = [li, ri].
For each queries[i]:
[li, ri] in nums.A Zero Array is an array where all elements are equal to 0.
Return true if it is possible to transform nums into a Zero Array after processing all the queries sequentially, otherwise return false.
Example 1:
Input: nums = [1,0,1], queries = [[0,2]]
Output: true
Explanation:
[0, 2] and decrement the values at these indices by 1.[0, 0, 0], which is a Zero Array.Example 2:
Input: nums = [4,3,2,1], queries = [[1,3],[0,2]]
Output: false
Explanation:
[1, 2, 3] and decrement the values at these indices by 1.[4, 2, 1, 0].[0, 1, 2] and decrement the values at these indices by 1.[3, 1, 0, 0], which is not a Zero Array.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 1051 <= queries.length <= 105queries[i].length == 20 <= li <= ri < nums.lengthclass Solution {
public boolean isZeroArray(int[] nums, int[][] queries) {
int n = nums.length, sweep = 0;
int[] transformationArr = new int[n + 1];
for (int[] q : queries) {
transformationArr[q[0]]--;
transformationArr[q[1] + 1]++;
}
for (int i = 0; i < n; i++) {
sweep += transformationArr[i];
if (sweep + nums[i] > 0)
return false;
}
return true;
}
}