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. PHP个人常用函数封装

    function GetIP(){ if(!empty($_SERVER["HTTP_CLIENT_IP"])){ $cip = $_SERVER["HTTP_CLIEN ...

  2. C#入门经典(1)

  3. HRBUST 1909 理工门外的树(双数组实现线段树功能)

    线段树固然可以做,但是在考虑线段树之前应该先考虑一下,这种用两个数组来解决问题的方法能不能行,如果行的话,就会为我们省很多时间,毕竟线段树代码量比较大 #include<iostream> ...

  4. 定制化jQuery

    毋庸置疑,jQuery很强大,很方便,但是......越来越臃肿,怎么办?,jquery只基于模块化开发的,可以通过工具定制jquery,选择你需要的模块即可. 下面这个网站可以帮你完成定制 http ...

  5. 在Android上实现SSL握手(客户端需要密钥和证书),实现服务器和客户端之间Socket交互

    Android的私钥和信任证书的格式必须是BKS格式的,通过配置本地JDK,让keytool可以生成BKS格式的私钥和信任证书,java本身没有BouncyCastle密库 服务端: Java代码  ...

  6. java类到底是如何加载并初始化的?

    Java虚拟机如何把编译好的.class文件加载到虚拟机里面?加载之后如何初始化类?静态类变量和实例类变量的初始化过程是否相同,分别是如何初始化的呢?这篇文章就 是解决上面3个问题的. 若有不正之处, ...

  7. JSP/JAVA目录清单

    JAVA253中国象棋(CS) JAVA258网络五子棋游戏的设计与实现(CS) JAVA390停车场管理系统SQL(CS) JSP001学生综合素质测评系统JAVA+Mysql JSP002学生成绩 ...

  8. hibernate--对象的三种状态Transient,Persistent,Detached

    Transient:刚new出来的对象 Persistent: sava或者update之后的状态 Detached: close或者clear之后 test函数: @Test public void ...

  9. 怎样让一个div高度自适应浏览器高度

    原文:http://www.jb51.net/web/79171.html 原文:http://zhidao.baidu.com/link?url=oId1sFRhiBnV37-RmRE6WQNHxi ...

  10. Python+Selenuim测试网站,只能打开Firefox浏览器却不能打开网页的解决方法

    最开始我使用的Selenium版本为2.48,Firefox版本为37,自动化打开网站的时候,可以正常打开. 后来由于Firefox的自检测更新,版本更新为47,导致版本不兼容,自动化打开网站浏览器时 ...