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)/得到连续 K 个 1 的最少相邻交换次数 [minimum-adjacent-swaps-for-k-consecutive-ones].html
2022-03-29 12:43:11 +08:00

37 lines
1.3 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> 。 <code>nums</code> 仅包含 <code>0</code> 和 <code>1</code> 。每一次移动,你可以选择 <strong>相邻</strong> 两个数字并将它们交换。</p>
<p>请你返回使 <code>nums</code> 中包含 <code>k</code> 个 <strong>连续 </strong><code>1</code> 的 <strong>最少</strong> 交换次数。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>nums = [1,0,0,1,0,1], k = 2
<b>输出:</b>1
<b>解释:</b>在第一次操作时nums 可以变成 [1,0,0,0,<strong>1</strong>,<strong>1</strong>] 得到连续两个 1 。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>nums = [1,0,0,0,0,0,1,1], k = 3
<b>输出:</b>5
<b>解释:</b>通过 5 次操作,最左边的 1 可以移到右边直到 nums 变为 [0,0,0,0,0,<strong>1</strong>,<strong>1</strong>,<strong>1</strong>] 。
</pre>
<p><strong>示例 3</strong></p>
<pre><b>输入:</b>nums = [1,1,0,1], k = 2
<b>输出:</b>0
<b>解释:</b>nums 已经有连续 2 个 1 了。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> 要么是 <code>0</code> ,要么是 <code>1</code> 。</li>
<li><code>1 &lt;= k &lt;= sum(nums)</code></li>
</ul>