Skip to content

Commit dd0d497

Browse files
committed
Sync LeetCode submission Runtime - 173 ms (25.19%), Memory - 21.7 MB (54.50%)
1 parent 2b30647 commit dd0d497

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p>
2+
3+
<p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p>
4+
5+
<ul>
6+
<li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li>
7+
</ul>
8+
9+
<p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p>
10+
11+
<p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, return -1.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<div class="example-block">
17+
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p>
18+
19+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
20+
21+
<p><strong>Explanation:</strong><br />
22+
We can do the following operations:</p>
23+
24+
<ul>
25+
<li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li>
26+
<li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li>
27+
<li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</li>
28+
</ul>
29+
</div>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<div class="example-block">
34+
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p>
35+
36+
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
37+
38+
<p><strong>Explanation:</strong><br />
39+
It is impossible to make all elements equal to 1.</p>
40+
</div>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
47+
<li><code>0 &lt;= nums[i] &lt;= 1</code></li>
48+
</ul>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Approach 1: Deque
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
from collections import deque
7+
8+
class Solution:
9+
def minOperations(self, nums: List[int]) -> int:
10+
flip_queue = deque()
11+
count = 0
12+
13+
for i in range(len(nums)):
14+
# Remove flips older than 3 indices
15+
while flip_queue and i > flip_queue[0] + 2:
16+
flip_queue.popleft()
17+
18+
if (nums[i] + len(flip_queue)) % 2 == 0:
19+
if i + 2 >= len(nums):
20+
return -1
21+
count += 1
22+
flip_queue.append(i)
23+
24+
return count
25+

0 commit comments

Comments
 (0)