其他分享
首页 > 其他分享> > 力扣练习——29 按要求补齐数组

力扣练习——29 按要求补齐数组

作者:互联网

1.问题描述

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

 

示例 1:

输入: nums = [1,3], n = 6

输出: 1 

解释:

根据 nums 里现有的组合 [1], [3], [1,3],可以得出 1, 3, 4。

现在如果我们将 2 添加到 nums 中, 组合变为: [1], [2], [3], [1,3], [2,3], [1,2,3]。

其和可以表示数字 1, 2, 3, 4, 5, 6,能够覆盖 [1, 6] 区间里所有的数。

所以我们最少需要添加一个数字。

 

示例 2:

输入: nums = [1,5,10], n = 20

输出: 2

解释: 我们需要添加 [2, 4]。

 

示例 3:

输入: nums = [1,2,2], n = 5

输出: 0

 

2.输入说明

首先输入数组长度m,然后输入m个整数。

最后输入一个正整数 n。

n不超过32位int型的表示范围,即:n<=2147483647。

3.输出说明

输出一个整数

4.范例

输入

3
1 5 10
20

输出

2

5.代码

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<set>
#include<stack>
using namespace std;

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

    //解题  https://leetcode.cn/problems/patching-array/solution/an-yao-qiu-bu-qi-shu-zu-by-leetcode-solu-klp1/
    //对于x,若[1,x-1]所有数字都被覆盖,且x在数组中,则[1,2*x-1]所有数字也被覆盖

    //1.定义
    long long total = 1;//累积的总和
    int index = 0;//遍历下标
    int count = 0;//需要补充的数字个数

    //2.遍历与处理
   
    //每次找到未被数组 nums 覆盖的最小的整数 x,在数组中补充 x,然后寻找下一个未被覆盖的最小的整数,重复上述步骤直到区间 [1,n] 中的所有数字都被覆盖。

    while (total <= sum)
    {
        if (index < nums.size() && nums[index] <= total )
        {
            total += nums[index++];
        }
        else 
        {
            total = total * 2;
            count++;//需要补充的个数加一
        }
    }

    return count;
}


 
int main()

{
    vector<int>nums;
    int m,tmp,n;
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> tmp;
        nums.push_back(tmp);
    }
    cin >> n;
    int res = min_Patches(nums, n);
    cout << res << endl;
    return 0;
}

 

标签:数字,nums,int,29,力扣,数组,include,补齐,输入
来源: https://www.cnblogs.com/juillard/p/16491544.html