Given a sorted integer array nums
and an integer n
, add/patch elements to the array such that any number in the range [1, n]
inclusive can be formed by the sum of some elements in the array.
Return the minimum number of patches required.
Example 1:
Input: nums = [1,3], n = 6 Output: 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.
Example 2:
Input: nums = [1,5,10], n = 20 Output: 2 Explanation: The two patches can be [2, 4].
Example 3:
Input: nums = [1,2,2], n = 5 Output: 0
Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 104
nums
is sorted in ascending order.1 <= n <= 231 - 1
Given a sorted integer array nums
and an integer n
, we need to find the minimum number of patches required to make sure that any number in the range [1, n]
(inclusive) can be formed by the sum of some elements in the array.
The solution employs a greedy approach. The core idea is to maintain a reach
variable. reach
represents the largest number we can currently form using the existing elements in nums
and any patches added so far. We iterate through the nums
array and the process is as follows:
Initialization: Start with reach = 0
and patches = 0
.
Iteration:
nums[i]
is less than or equal to reach + 1
, it means we can extend our reach. We update reach
to reach + nums[i]
. We can form all numbers up to reach
.nums[i]
is greater than reach + 1
, it means there's a gap. We need to add a patch of size reach + 1
to cover this gap. This extends our reach to reach + reach + 1 = 2 * reach + 1
. We increment patches
.Termination: The loop continues until reach
becomes greater than or equal to n
. At this point, we can form all numbers in the range [1, n]
, and the value of patches
represents the minimum number of patches needed.
nums
. The algorithm iterates through nums
once, and the while
loop's iterations are bounded by the logarithmic growth of reach
.class Solution:
def minPatches(self, nums: List[int], n: int) -> int:
patches = 0
reach = 0
i = 0
while reach < n:
if i < len(nums) and nums[i] <= reach + 1:
reach += nums[i]
i += 1
else:
patches += 1
reach += reach + 1 # Add a patch of size reach + 1
return patches
class Solution {
public int minPatches(int[] nums, int n) {
int patches = 0;
long reach = 0; // Use long to avoid integer overflow
int i = 0;
while (reach < n) {
if (i < nums.length && nums[i] <= reach + 1) {
reach += nums[i];
i++;
} else {
patches++;
reach += reach + 1;
}
}
return patches;
}
}
class Solution {
public:
int minPatches(vector<int>& nums, int n) {
int patches = 0;
long long reach = 0; // Use long long to avoid integer overflow
int i = 0;
while (reach < n) {
if (i < nums.size() && nums[i] <= reach + 1) {
reach += nums[i];
i++;
} else {
patches++;
reach += reach + 1;
}
}
return patches;
}
};
Note that in Java and C++, long
or long long
are used for reach
to prevent potential integer overflow issues, especially when n
is large. The Python implementation implicitly handles large integers. The logic remains consistent across all languages.