-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3355-zero-array-transformation-i.js
More file actions
43 lines (39 loc) · 1.11 KB
/
3355-zero-array-transformation-i.js
File metadata and controls
43 lines (39 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* 3355. Zero Array Transformation I
* https://leetcode.com/problems/zero-array-transformation-i/
* Difficulty: Medium
*
* You are given an integer array nums of length n and a 2D array queries, where
* queries[i] = [li, ri].
*
* For each queries[i]:
* - Select a subset of indices within the range [li, ri] in nums.
* - Decrement the values at the selected indices by 1.
*
* 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.
*/
/**
* @param {number[]} nums
* @param {number[][]} queries
* @return {boolean}
*/
var isZeroArray = function(nums, queries) {
const maxDecrements = new Array(nums.length).fill(0);
for (const [left, right] of queries) {
maxDecrements[left]++;
if (right + 1 < nums.length) {
maxDecrements[right + 1]--;
}
}
let currentDecrements = 0;
for (let i = 0; i < nums.length; i++) {
currentDecrements += maxDecrements[i];
if (nums[i] > currentDecrements) {
return false;
}
}
return true;
};