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. android4.0蓝牙使能的详细解析(转)

    源:http://www.cnblogs.com/xiaochao1234/p/3818193.html 本文详细分析了android4.0 中蓝牙使能的过程,相比较android2.3,4.0中的蓝 ...

  2. CodeForces 158DIce Sculptures(枚举)

    一个暴力的枚举,枚举组成正多边形需要对应覆盖原先的几条边,范围为(1,n/3),然后维护最大值就可以了,注意初始化为-inf. #include<stdio.h> #include< ...

  3. PAT (Advanced Level) 1059. Prime Factors (25)

    素因子分解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

  4. P3P解决cookie存取的跨域问题

    最近在做一个流量统计的东西的时候,偶然发现IE在对iframe里面的页面写Cookie的时候有一些安全限制,导致读取Cookie不成功,找了好长时间的解决办法,重要找到如下的办法: 1.页面里的COO ...

  5. 利用html5压缩图片,产出base64图片

    /* 将页面选择的图片等比压缩成指定大小(长边固定) file:图片文件 callBack:回调函数 maxLen:长边的长度*/function makePic(file,callBack,maxL ...

  6. Netbeans 6.8 + apktool_2.0.0b9 动态调试smali文件

    前言 很早就知道用Netbeans能够单步调试smali,一直拖到现在才真正的自己实现了一次~ 下面是详细步骤! 0×1 环境及工具 a.apktool_2.0.0b9 下载地址:http://con ...

  7. VPN工作原理

    引言 在过去几十年中,世界发生了很大的变化.现在很多公司除了处理本地或地区性事务外,还要考虑全球市场和物流的问题.很多公司在全国甚至全球都设有分支机构,而这些公司都需要做的一件事情就是:找到能够与分公 ...

  8. 我的git常用命令

    git add . # 将所有修改过的工作文件提交暂存区 git commit -m ""

  9. php 注意点

    1.如果一个方法可静态化,就对它做静态声明.速率可提升至4倍. 2.echo 比 print 快. 3.使用echo的多重参数(译注:指用逗号而不是句点)代替字符串连接. 4.在执行for循环之前确定 ...

  10. Mysql基于GTID主从复制

    Mysql5.6基于GTID全局事务的复制 什么是GTID?   GTID(Global Transaction Identifiers)是全局事务标识 当使用GTIDS时,在主上提交的每一个事务都会 ...