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 连续位的最小翻转次数 [minimum-number-of-k-consecutive-bit-flips].html
2022-03-29 12:43:11 +08:00

46 lines
1.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>给定一个二进制数组 <code>nums</code> 和一个整数 <code>k</code></p>
<p><strong>k位翻转</strong> 就是从 <code>nums</code> 中选择一个长度为 <code>k</code><strong>子数组</strong> ,同时把子数组中的每一个 <code>0</code> 都改成 <code>1</code> ,把子数组中的每一个 <code>1</code> 都改成 <code>0</code></p>
<p>返回数组中不存在 <code>0</code> 所需的最小 <strong>k位翻转</strong> 次数。如果不可能,则返回&nbsp;<code>-1</code>&nbsp;</p>
<p><strong>子数组</strong> 是数组的 <strong>连续</strong> 部分。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [0,1,0], K = 1
<strong>输出:</strong>2
<strong>解释:</strong>先翻转 A[0],然后翻转 A[2]。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1,1,0], K = 2
<strong>输出:</strong>-1
<strong>解释:</strong>无论我们怎样翻转大小为 2 的子数组,我们都不能使数组变为 [1,1,1]。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [0,0,0,1,0,1,1,0], K = 3
<strong>输出:</strong>3
<strong>解释:</strong>
翻转 A[0],A[1],A[2]:&nbsp;A变成 [1,1,1,1,0,1,1,0]
翻转 A[4],A[5],A[6]:&nbsp;A变成 [1,1,1,1,1,0,0,0]
翻转 A[5],A[6],A[7]:&nbsp;A变成 [1,1,1,1,1,1,1,1]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>