Skip to content

Commit 2ed286a

Browse files
committed
Add solution #3355
1 parent 3298f0c commit 2ed286a

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,883 LeetCode solutions in JavaScript
1+
# 1,884 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1877,6 +1877,7 @@
18771877
3341|[Find Minimum Time to Reach Last Room I](./solutions/3341-find-minimum-time-to-reach-last-room-i.js)|Medium|
18781878
3342|[Find Minimum Time to Reach Last Room II](./solutions/3342-find-minimum-time-to-reach-last-room-ii.js)|Medium|
18791879
3343|[Count Number of Balanced Permutations](./solutions/3343-count-number-of-balanced-permutations.js)|Hard|
1880+
3355|[Zero Array Transformation I](./solutions/3355-zero-array-transformation-i.js)|Medium|
18801881
3356|[Zero Array Transformation II](./solutions/3356-zero-array-transformation-ii.js)|Medium|
18811882
3375|[Minimum Operations to Make Array Values Equal to K](./solutions/3375-minimum-operations-to-make-array-values-equal-to-k.js)|Easy|
18821883
3392|[Count Subarrays of Length Three With a Condition](./solutions/3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 3355. Zero Array Transformation I
3+
* https://leetcode.com/problems/zero-array-transformation-i/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array nums of length n and a 2D array queries, where
7+
* queries[i] = [li, ri].
8+
*
9+
* For each queries[i]:
10+
* - Select a subset of indices within the range [li, ri] in nums.
11+
* - Decrement the values at the selected indices by 1.
12+
*
13+
* A Zero Array is an array where all elements are equal to 0.
14+
*
15+
* Return true if it is possible to transform nums into a Zero Array after processing all the
16+
* queries sequentially, otherwise return false.
17+
*/
18+
19+
/**
20+
* @param {number[]} nums
21+
* @param {number[][]} queries
22+
* @return {boolean}
23+
*/
24+
var isZeroArray = function(nums, queries) {
25+
const maxDecrements = new Array(nums.length).fill(0);
26+
27+
for (const [left, right] of queries) {
28+
maxDecrements[left]++;
29+
if (right + 1 < nums.length) {
30+
maxDecrements[right + 1]--;
31+
}
32+
}
33+
34+
let currentDecrements = 0;
35+
for (let i = 0; i < nums.length; i++) {
36+
currentDecrements += maxDecrements[i];
37+
if (nums[i] > currentDecrements) {
38+
return false;
39+
}
40+
}
41+
42+
return true;
43+
};

0 commit comments

Comments
 (0)