Skip to content

Commit 67cb405

Browse files
committed
Sync LeetCode submission Runtime - 87 ms (49.85%), Memory - 55.6 MB (27.67%)
1 parent 7d1d401 commit 67cb405

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<p>You are given an integer array <code>nums</code> of length <code>n</code> and a 2D array <code>queries</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
2+
3+
<p>For each <code>queries[i]</code>:</p>
4+
5+
<ul>
6+
<li>Select a <span data-keyword="subset">subset</span> of indices within the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> in <code>nums</code>.</li>
7+
<li>Decrement the values at the selected indices by 1.</li>
8+
</ul>
9+
10+
<p>A <strong>Zero Array</strong> is an array where all elements are equal to 0.</p>
11+
12+
<p>Return <code>true</code> if it is <em>possible</em> to transform <code>nums</code> into a <strong>Zero Array </strong>after processing all the queries sequentially, otherwise return <code>false</code>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<div class="example-block">
18+
<p><strong>Input:</strong> <span class="example-io">nums = [1,0,1], queries = [[0,2]]</span></p>
19+
20+
<p><strong>Output:</strong> <span class="example-io">true</span></p>
21+
22+
<p><strong>Explanation:</strong></p>
23+
24+
<ul>
25+
<li><strong>For i = 0:</strong>
26+
27+
<ul>
28+
<li>Select the subset of indices as <code>[0, 2]</code> and decrement the values at these indices by 1.</li>
29+
<li>The array will become <code>[0, 0, 0]</code>, which is a Zero Array.</li>
30+
</ul>
31+
</li>
32+
</ul>
33+
</div>
34+
35+
<p><strong class="example">Example 2:</strong></p>
36+
37+
<div class="example-block">
38+
<p><strong>Input:</strong> <span class="example-io">nums = [4,3,2,1], queries = [[1,3],[0,2]]</span></p>
39+
40+
<p><strong>Output:</strong> <span class="example-io">false</span></p>
41+
42+
<p><strong>Explanation:</strong></p>
43+
44+
<ul>
45+
<li><strong>For i = 0:</strong>
46+
47+
<ul>
48+
<li>Select the subset of indices as <code>[1, 2, 3]</code> and decrement the values at these indices by 1.</li>
49+
<li>The array will become <code>[4, 2, 1, 0]</code>.</li>
50+
</ul>
51+
</li>
52+
<li><strong>For i = 1:</strong>
53+
<ul>
54+
<li>Select the subset of indices as <code>[0, 1, 2]</code> and decrement the values at these indices by 1.</li>
55+
<li>The array will become <code>[3, 1, 0, 0]</code>, which is not a Zero Array.</li>
56+
</ul>
57+
</li>
58+
</ul>
59+
</div>
60+
61+
<p>&nbsp;</p>
62+
<p><strong>Constraints:</strong></p>
63+
64+
<ul>
65+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
66+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
67+
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
68+
<li><code>queries[i].length == 2</code></li>
69+
<li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt; nums.length</code></li>
70+
</ul>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Approach: Difference Array
2+
3+
# n = len(nums), m = len(queries)
4+
# Time: O(n + m)
5+
# Space: O(n)
6+
7+
class Solution:
8+
def isZeroArray(self, nums: List[int], queries: List[List[int]]) -> bool:
9+
delta_array = [0] * (len(nums) + 1)
10+
for left, right in queries:
11+
delta_array[left] += 1
12+
delta_array[right + 1] -= 1
13+
14+
operation_count = []
15+
curr_operations = 0
16+
17+
for delta in delta_array:
18+
curr_operations += delta
19+
operation_count.append(curr_operations)
20+
21+
for operations, target in zip(operation_count, nums):
22+
if operations < target:
23+
return False
24+
25+
return True
26+

0 commit comments

Comments
 (0)