# 按要求补齐数组

<p>给定一个已排序的正整数数组 <em>nums，</em>和一个正整数&nbsp;<em>n 。</em>从&nbsp;<code>[1, n]</code>&nbsp;区间内选取任意个数字补充到&nbsp;<em>nums&nbsp;</em>中，使得&nbsp;<code>[1, n]</code>&nbsp;区间内的任何数字都可以用&nbsp;<em>nums&nbsp;</em>中某几个数字的和来表示。请输出满足上述要求的最少需要补充的数字个数。</p>

<p><strong>示例&nbsp;1:</strong></p>

<pre><strong>输入: </strong><em>nums</em> = <code>[1,3]</code>, <em>n</em> = <code>6</code>
<strong>输出: </strong>1 
<strong>解释:</strong>
根据<em> nums&nbsp;</em>里现有的组合&nbsp;<code>[1], [3], [1,3]</code>，可以得出&nbsp;<code>1, 3, 4</code>。
现在如果我们将&nbsp;<code>2</code>&nbsp;添加到&nbsp;<em>nums 中，</em>&nbsp;组合变为: <code>[1], [2], [3], [1,3], [2,3], [1,2,3]</code>。
其和可以表示数字&nbsp;<code>1, 2, 3, 4, 5, 6</code>，能够覆盖&nbsp;<code>[1, 6]</code>&nbsp;区间里所有的数。
所以我们最少需要添加一个数字。</pre>

<p><strong>示例 2:</strong></p>

<pre><strong>输入: </strong><em>nums</em> = <code>[1,5,10]</code>, <em>n</em> = <code>20</code>
<strong>输出:</strong> 2
<strong>解释: </strong>我们需要添加&nbsp;<code>[2, 4]</code>。
</pre>

<p><strong>示例&nbsp;3:</strong></p>

<pre><strong>输入: </strong><em>nums</em> = <code>[1,2,2]</code>, <em>n</em> = <code>5</code>
<strong>输出:</strong> 0
</pre>

<p>以下错误的选项是？</p>

## aop

### before

```cpp
#include <bits/stdc++.h>
using namespace std;

```
### after

```cpp
int main()
{
    Solution sol;
    vector<int> nums = {1, 5, 10};
    int n = 20;
    int res = sol.minPatches(nums, n);
    cout << res;

    return 0;
}
```

## 答案

```cpp
class Solution
{
public:
    int minPatches(vector<int> &nums, int n)
    {
        long sum = 0;
        int reti = 0;
        int i = 0;
        while (sum < n)
        {
            if (i >= nums.size())
                break;
            if ((sum + 1) >= nums[i])
            {
                sum += nums[++i];
            }
            else
            {
                int temp = sum + 1;
                sum += temp;
                reti++;
            }
        }
        while (sum < n)
        {
            int temp = sum + 1;
            sum += temp;
        }
        return reti;
    }
};
```

## 选项


### A

```cpp
class Solution
{
public:
    int minPatches(vector<int> &nums, int n)
    {
        int resultCnt = 0;
        int numsSize = nums.size(), index = 0;
        long long head = 1;
        while (head <= n)
        {
            if (index < numsSize && nums[index] <= head)
            {
                head += nums[index++];
            }
            else
            {
                head += head;
                resultCnt += 1;
            }
        }
        return resultCnt;
    }
};

```

### B

```cpp
class Solution
{
public:
    int minPatches(vector<int> &nums, int n)
    {
        int patchCount = 0, i = 0;
        long miss = 1;
        while (miss <= n)
        {
            if (i < nums.size() && miss >= nums[i])
                miss += nums[i++];
            else
            {
                patchCount++;
                miss = miss * 2;
            }
        }
        return patchCount;
    }
};
```

### C

```cpp
class Solution
{
public:
    int minPatches(vector<int> &nums, int n)
    {
        int len = nums.size(), i = 0;
        long current_coverage = 0;
        int ans = 0;
        while (current_coverage < n)
        {
            if (i < len)
            {
                if (nums[i] <= current_coverage + 1)
                {
                    current_coverage += nums[i];
                    i++;
                }
                else
                {
                    ans++;
                    current_coverage += current_coverage + 1;
                }
            }
            else
            {
                ans++;
                current_coverage += current_coverage + 1;
            }
        }
        return ans;
    }
};
```
