Skip to content

Commit 52620f4

Browse files
authored
Create Zero Array Transformation I.java
1 parent 0209afb commit 52620f4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public boolean isZeroArray(int[] nums, int[][] queries) {
3+
int n = nums.length;
4+
int[] diff = new int[n + 1];
5+
for (int[] query : queries) {
6+
int left = query[0];
7+
int right = query[1];
8+
diff[left] += 1;
9+
diff[right + 1] -= 1;
10+
}
11+
int[] operation = new int[n + 1];
12+
int currOperation = 0;
13+
for (int i = 0; i < n + 1; i++) {
14+
currOperation += diff[i];
15+
operation[i] = currOperation;
16+
}
17+
for (int i = 0; i < n; i++) {
18+
if (operation[i] < nums[i]) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
}

0 commit comments

Comments
 (0)