Skip to content

Commit 505e816

Browse files
committed
Sync LeetCode submission Runtime - 220 ms (77.73%), Memory - 62.5 MB (13.54%)
1 parent fc231ab commit 505e816

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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>Each <code>queries[i]</code> represents the following action on <code>nums</code>:</p>
4+
5+
<ul>
6+
<li>Decrement the value at each index in the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> in <code>nums</code> by <strong>at most</strong><strong> </strong>1.</li>
7+
<li>The amount by which the value is decremented can be chosen <strong>independently</strong> for each index.</li>
8+
</ul>
9+
10+
<p>A <strong>Zero Array</strong> is an array with all its elements equal to 0.</p>
11+
12+
<p>Return the <strong>maximum </strong>number of elements that can be removed from <code>queries</code>, such that <code>nums</code> can still be converted to a <strong>zero array</strong> using the <em>remaining</em> queries. If it is not possible to convert <code>nums</code> to a <strong>zero array</strong>, return -1.</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 = [2,0,2], queries = [[0,2],[0,2],[1,1]]</span></p>
19+
20+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
21+
22+
<p><strong>Explanation:</strong></p>
23+
24+
<p>After removing <code>queries[2]</code>, <code>nums</code> can still be converted to a zero array.</p>
25+
26+
<ul>
27+
<li>Using <code>queries[0]</code>, decrement <code>nums[0]</code> and <code>nums[2]</code> by 1 and <code>nums[1]</code> by 0.</li>
28+
<li>Using <code>queries[1]</code>, decrement <code>nums[0]</code> and <code>nums[2]</code> by 1 and <code>nums[1]</code> by 0.</li>
29+
</ul>
30+
</div>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<div class="example-block">
35+
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1], queries = [[1,3],[0,2],[1,3],[1,2]]</span></p>
36+
37+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
38+
39+
<p><strong>Explanation:</strong></p>
40+
41+
<p>We can remove <code>queries[2]</code> and <code>queries[3]</code>.</p>
42+
</div>
43+
44+
<p><strong class="example">Example 3:</strong></p>
45+
46+
<div class="example-block">
47+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], queries = [[0,3]]</span></p>
48+
49+
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
50+
51+
<p><strong>Explanation:</strong></p>
52+
53+
<p><code>nums</code> cannot be converted to a zero array even after using all the queries.</p>
54+
</div>
55+
56+
<p>&nbsp;</p>
57+
<p><strong>Constraints:</strong></p>
58+
59+
<ul>
60+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
61+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
62+
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
63+
<li><code>queries[i].length == 2</code></li>
64+
<li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt; nums.length</code></li>
65+
</ul>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Approach: Greedy + Priority Queue
2+
3+
class Solution:
4+
def maxRemoval(self, nums: List[int], queries: List[List[int]]) -> int:
5+
queries.sort(key=lambda x: x[0])
6+
heap = []
7+
delta_array = [0] * (len(nums) + 1)
8+
operations = 0
9+
j = 0
10+
11+
for i, num in enumerate(nums):
12+
operations += delta_array[i]
13+
while j < len(queries) and queries[j][0] == i:
14+
heapq.heappush(heap, -queries[j][1])
15+
j += 1
16+
while operations < num and heap and -heap[0] >= i:
17+
operations += 1
18+
delta_array[-heappop(heap) + 1] -= 1
19+
if operations < num:
20+
return -1
21+
22+
return len(heap)
23+

0 commit comments

Comments
 (0)