2022-03-27 20:56:26 +08:00
< 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 >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:56:26 +08:00
< 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 >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:56:26 +08:00
< pre >
< strong > Input:< / strong > nums = [1,5,10], n = 20
< strong > Output:< / strong > 2
Explanation: The two patches can be [2, 4].
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 3:< / strong > < / p >
2022-03-27 20:56:26 +08:00
< 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 >