mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
49 lines
2.0 KiB
HTML
49 lines
2.0 KiB
HTML
|
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p>
|
||
|
|
||
|
<p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p>
|
||
|
|
||
|
<ul>
|
||
|
<li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li>
|
||
|
</ul>
|
||
|
|
||
|
<p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p>
|
||
|
|
||
|
<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>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong class="example">Example 1:</strong></p>
|
||
|
|
||
|
<div class="example-block">
|
||
|
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1,0,0]</span></p>
|
||
|
|
||
|
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||
|
|
||
|
<p><strong>Explanation:</strong><br />
|
||
|
We can do the following operations:</p>
|
||
|
|
||
|
<ul>
|
||
|
<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>
|
||
|
<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>
|
||
|
<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>
|
||
|
</ul>
|
||
|
</div>
|
||
|
|
||
|
<p><strong class="example">Example 2:</strong></p>
|
||
|
|
||
|
<div class="example-block">
|
||
|
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p>
|
||
|
|
||
|
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||
|
|
||
|
<p><strong>Explanation:</strong><br />
|
||
|
It is impossible to make all elements equal to 1.</p>
|
||
|
</div>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Constraints:</strong></p>
|
||
|
|
||
|
<ul>
|
||
|
<li><code>3 <= nums.length <= 10<sup>5</sup></code></li>
|
||
|
<li><code>0 <= nums[i] <= 1</code></li>
|
||
|
</ul>
|