mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 19:53:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			46 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			46 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<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> 次数。如果不可能,则返回 <code>-1</code> 。</p>
 | 
						||
 | 
						||
<p><strong>子数组</strong> 是数组的 <strong>连续</strong> 部分。</p>
 | 
						||
 | 
						||
<p> </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]: A变成 [1,1,1,1,0,1,1,0]
 | 
						||
翻转 A[4],A[5],A[6]: A变成 [1,1,1,1,1,0,0,0]
 | 
						||
翻转 A[5],A[6],A[7]: A变成 [1,1,1,1,1,1,1,1]
 | 
						||
</pre>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
 | 
						||
	<li><code>1 <= k <= nums.length</code></li>
 | 
						||
</ul>
 |