mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			42 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>Given a sorted integer array <code>nums</code> and an integer <code>n</code>, add/patch elements to the array such that any number in the range <code>[1, n]</code> inclusive can be formed by the sum of some elements in the array.</p>
 | 
						|
 | 
						|
<p>Return <em>the minimum number of patches required</em>.</p>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Example 1:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> nums = [1,3], n = 6
 | 
						|
<strong>Output:</strong> 1
 | 
						|
Explanation:
 | 
						|
Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
 | 
						|
Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
 | 
						|
Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
 | 
						|
So we only need 1 patch.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong>Example 2:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> nums = [1,5,10], n = 20
 | 
						|
<strong>Output:</strong> 2
 | 
						|
Explanation: The two patches can be [2, 4].
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong>Example 3:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> nums = [1,2,2], n = 5
 | 
						|
<strong>Output:</strong> 0
 | 
						|
</pre>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= nums.length <= 1000</code></li>
 | 
						|
	<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
 | 
						|
	<li><code>nums</code> is sorted in <strong>ascending order</strong>.</li>
 | 
						|
	<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
 | 
						|
</ul>
 |