1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/使二进制数组全部等于 1 的最少操作次数 II [minimum-operations-to-make-binary-array-elements-equal-to-one-ii].html
2024-06-25 01:21:44 +08:00

56 lines
2.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个二进制数组&nbsp;<code>nums</code>&nbsp;</p>
<p>你可以对数组执行以下操作&nbsp;<strong>任意</strong>&nbsp;次(也可以 0 次):</p>
<ul>
<li>选择数组中 <strong>任意</strong>&nbsp;一个下标 <code>i</code>&nbsp;,并将从下标 <code>i</code>&nbsp;开始一直到数组末尾 <strong>所有</strong>&nbsp;元素 <strong>反转</strong>&nbsp;</li>
</ul>
<p><b>反转</b>&nbsp;一个元素指的是将它的值从 0 变 1 ,或者从 1 变 0 。</p>
<p>请你返回将 <code>nums</code>&nbsp;中所有元素变为 1 的 <strong>最少</strong>&nbsp;操作次数。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [0,1,1,0,1]</span></p>
<p><span class="example-io"><b>输出:</b>4</span></p>
<p><strong>解释:</strong><br />
我们可以执行以下操作:</p>
<ul>
<li>选择下标&nbsp;<code>i = 1</code>&nbsp;执行操作,得到<span class="example-io">&nbsp;<code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>&nbsp;</span></li>
<li>选择下标&nbsp;<code>i = 0</code>&nbsp;执行操作,得到<span class="example-io">&nbsp;<code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>&nbsp;</span></li>
<li>选择下标&nbsp;<code>i = 4</code>&nbsp;执行操作,得到<span class="example-io">&nbsp;<code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>&nbsp;</span></li>
<li>选择下标&nbsp;<code>i = 3</code>&nbsp;执行操作,得到<span class="example-io">&nbsp;<code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>&nbsp;</span></li>
</ul>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [1,0,0,0]</span></p>
<p><span class="example-io"><b>输出:</b>1</span></p>
<p><strong>解释:</strong><br />
我们可以执行以下操作:</p>
<ul>
<li>选择下标&nbsp;<code>i = 1</code>&nbsp;执行操作,得到<span class="example-io">&nbsp;<code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>&nbsp;</span></li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= nums[i] &lt;= 1</code></li>
</ul>