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.

Credits:
Special thanks to @dietpepsi for adding this problem and creating
all test cases.

Subscribe to see which companies asked this
question

思路:我们依次将数组中缺少的数字加入数组nums.设置当前数字组合相加的范围是[1,total),total初始设置为1.当nums[i]存在且nums[i]<=total时,相加的范围可以拓展到[1,total+nums[i]).nums[i]最大可以等于total,否则我们利用贪心的策略,尽可能快地提高total的值,向数组nums中插入total。重复上述循环直至total>n.最终增加的数的个数就是数组nums大小的变化。

class Solution {
public:
int minPatches(vector<int>& nums, int n) {
int N =nums.size();
long total =;
int i=;
while(total<=n){
if(i<nums.size() &&nums[i]<=total){
total+=nums[i++];
}
else{
nums.insert(nums.begin()+i,total);
}
}
return nums.size()-N;
}
};

330. Patching Array的更多相关文章

  1. [LeetCode] 330. Patching Array 数组补丁

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  2. [LeetCode] Patching Array 补丁数组

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  3. Patching Array

    引用原文:http://blog.csdn.net/murmured/article/details/50596403 但感觉原作者的解释中存在一些错误,这里加了一些自己的理解 Given a sor ...

  4. LeetCode Patching Array

    原题链接在这里:https://leetcode.com/problems/patching-array/ 题目: Given a sorted positive integer array nums ...

  5. 330. Patching Array--Avota

    问题描述: Given a sorted positive integer array nums and an integer n, add/patch elements to the array s ...

  6. [Swift]LeetCode330. 按要求补齐数组 | Patching Array

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  7. LeetCode-330.Patching Array

    /** * nums的所有元素,假设最大能连续形成[1,sum] 当增加一个element的时候 * 会变成 [1,sum] [element+1,sum+element]两个区间,这两个区间有以下可 ...

  8. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  9. LeetCode题目按公司分类

    LinkedIn(39) 1 Two Sum 23.0% Easy 21 Merge Two Sorted Lists 35.4% Easy 23 Merge k Sorted Lists 23.3% ...

随机推荐

  1. 向页面动态添加外部js

    var script = document.createElement("script"); script.src = '/static/js/view/'+url+'.js'; ...

  2. 如何在Apache中配置多端口访问

    环境: Windows server 2008 R2, Apache, PHP5 步骤: 建立一个目录,里面放置一个index.php. 打开Apache\conf\httpd.conf 文件,做如下 ...

  3. Eclipse中Ctrl+Alt+Down和Ctrl+Alt+Up不起作用

    不起作用是因为跟因特尔的快捷键冲突. 1.在桌面上右键,选择“图形属性......” 2.选择“选项和支持” 3.更改快捷键. 注意:单纯禁用英特尔的快捷键可能不起作用.

  4. stock 仓位

    别胆大求多,不轻信盲从.抓住几只自己长期关注并看好的股票.将这几只股票选为自选股,而其他股,不管是机构推荐还是股评荐股,都要谨慎,不轻易听从. 巧用“三三制”,根据趋势控制仓位.当不知道是在涨还是在跌 ...

  5. zf-关于更改账号密码的问题

    一般项目的数据库里都会有一个 SYS_USER表 里面有账号密码 一般 202……70 的都是123加密后的字符串 如果碰到项目运行之后不知道登陆密码的时候 可以在数据库中 把USER_PASS 改成 ...

  6. angular Jsonp的坑

    angular 为了解决跨域问题 一些第三方接口会提供jsonp来调用,需要使用callback=JSON_CALLBACK来处理 这个时候问题来了,有些借口是不支持callback里面带有点语法的, ...

  7. WINCE下进程间通信(一)

    WINCE下进程间通信(一) 在WINCE开发中经常需要在不同的进程之间传递.共享数据,总结了一下,WINCE下进程间通信常用的方式有:Windows消息,共享内存,socket通信,管道,全局原子, ...

  8. POJ 3648 Wedding

    2-SAT,直接选择新娘一侧的比较难做,所以处理的时候选择新郎一侧的,最后反着输出就可以. A和B通奸的话,就建边 A->B'以及B->A’,表示 A在新郎一侧的话,B一定不在:B在新郎一 ...

  9. android数据库sqlite增加删改查

    http://hi-beijing.iteye.com/blog/1322040 http://www.cnblogs.com/wenjiang/archive/2013/05/28/3100860. ...

  10. Asp获取网址相关参数大全

      Asp获取网址相关参数大全 代码一:[获取地址中的文件名,不包含扩展名]<%dim Url,FileName,File Url=split(request.servervariables(& ...