330. Patching Array 给数组添上元素,使得满足存在所有1~n元素
来源:互联网 发布:淘宝限时促销关键词 编辑:程序博客网 时间:2024/05/20 06:09
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in 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:
nums = [1, 3]
, n = 6
Return 1
.
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:
nums = [1, 5, 10]
, n = 20
Return 2
.
The two patches can be [2, 4]
.
Example 3:
nums = [1, 2, 2]
, n = 5
Return 0
.
解答:
1.我的想法 暴力
对于给定一个数组,元素之间进行相加,然后进行排序,从头到尾进行遍历,发现不存在的元素就加入,但是时间复杂度太高了,超时了,,,,
class Solution {public:void add(vector<int>& s, int num, int n){ int len = s.size(); for(int it = 0; it < len; it++){ int res = s[it] + num; if(res <= n) s.push_back(res); } s.push_back(num);}int minPatches(vector<int>& nums, int n) { vector<int>s; int count = 0; if(nums.size() > 0){ s.push_back(nums[0]); for(int i = 1; i< nums.size(); i++) add(s, nums[i], n); } else{ add(s, 1, n); count++; } sort(s.begin(), s.end()); if(s.size() == n) return 0; while(true){ int maxn = 0; for(int i = 1; i <=n; i++){ if(s[i-1] != i){ maxn = i; break; } } add(s, maxn, n); count++; sort(s.begin(), s.end()); s.erase(unique(s.begin(), s.end()),s.end()); if(s.size() == n) break; } return count;}};
2.别人的解答 用贪心的方法
比如:开始时最大值为0,从num的第0个元素开始遍历
nums=[1 2 5 6 20], n = 50.
第一次时,若元素<= max+1,则将此元素加入max中;【1~1】
加入了1,则max=1,那么下一个要加入的就是2(max+1),如果下一个元素满足<=max+1,那么就把该元素加入,则此时的数组【1,2】中能达到的最大值max=3; 【1~13】
那么下一个要加入的就是4,但是此时4<5,说明数组里没有4,此时需要把4加入max,同时count++; 那么此时最大值max=3+4=7,数组中有【1,2,4】;【1~7】
下一个要加入的就是max+1=8,此时 5<8,可以把范围缩小为5+max=12=max;【1~12】
下一个要加入12+1=13,此时6<13,那么范围扩大为6+max=18;此时数组为【1,2,4,5,6】 【1~18】
下一个要加入的就是18+1=19,此时19<20,那么需要加入19,此时max=19+18=37,count++; 【1~37】
下一个要加入的是38,此时20<38,只需扩大范围20+37=57,到此57>n=50,程序结束。【1~57】
程序如下:
class Solution {public: int minPatches(vector<int>& nums, int n) { int cnt = 0, i = 0, len = nums.size(); long long maxnum = 0; while(maxnum < n){ if(i < len && nums[i] <= maxnum + 1){ maxnum += nums[i]; i++; }else{ maxnum += (maxnum + 1); cnt++; } } return cnt; }};
- 330. Patching Array 给数组添上元素,使得满足存在所有1~n元素
- 求出数组中满足给定和的所有元素组合
- 获得一个2n数组中的N个元素,使得该N个元素与剩余元素和最小
- 证明偶阶群必存在元素a,使得a^2=1
- 假设一整型数组存在若干正数和负数,现在通过某种算法使得该数组的所有负数在正数的左边,且保证负数和正数间元素相对位置不变。时空复杂度要求分别为:o(n)和o(1)。
- Shell 数组元素个数${#array[@]} 数组的所有元素${array[*]} 字符串长度${#str}
- @[元素1,元素2,...元素n]构造数组
- 重排数组元素使得所有的奇数位于所有偶数之前
- n个sorted数组,每个数组n个元素,求所有元素中值
- 330. Patching Array-数组补丁|贪心算法
- LeetCode 330. Patching Array(数组补丁)
- 求出n个互异的数使得最小公倍数等于所有元素之和
- 打印数组所有元素
- 华为机试——找出一个数组中满足2^n的元素个数
- 给定数组Arr[n],O(n)时间内找出每个元素左侧所有元素中位置最靠近该元素且大于该元素的元素
- 给定数组Arr[n],O(n)时间内找出每个元素左侧所有元素中位置最靠近该元素且大于该元素的元素
- 给定数组Arr[n],O(n)时间内找出每个元素左侧所有元素中位置最靠近该元素且大于该元素的元素
- 取值为[1,n-1]含n个元素的整数数组至少存在一个重复数,找出一个重复元素
- ssh-keygen生成密钥设置方法,让ubuntu无密码登录linux
- Linux下wc命令详解
- xpath 获取多级子标签完整文档
- 分享一款简单好用的接口文档管理工具
- 关于js向java后台传日期参数的问题
- 330. Patching Array 给数组添上元素,使得满足存在所有1~n元素
- mysql5.6升级到5.7后Sequel Pro无法连接解决
- linux驱动中probe函数的返回值
- 前端开发之--Emmet 组合快捷键加快开发效率(一)
- mongDB分片
- Unity ZTest 深度测试 & ZWrite 深度写入
- leetcode229. Majority Element II
- 《剑指offer》-构建乘积数组
- ubuntu 安装配置nginx和(php再下一篇文章中,因为实在受不了csdn的markdown编辑器了)