330. Patching Array
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,, which now covers the range
2, 3, 4, 5, 6[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的更多相关文章
- [LeetCode] 330. Patching Array 数组补丁
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- [LeetCode] Patching Array 补丁数组
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- Patching Array
引用原文:http://blog.csdn.net/murmured/article/details/50596403 但感觉原作者的解释中存在一些错误,这里加了一些自己的理解 Given a sor ...
- LeetCode Patching Array
原题链接在这里:https://leetcode.com/problems/patching-array/ 题目: Given a sorted positive integer array nums ...
- 330. Patching Array--Avota
问题描述: Given a sorted positive integer array nums and an integer n, add/patch elements to the array s ...
- [Swift]LeetCode330. 按要求补齐数组 | Patching Array
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- LeetCode-330.Patching Array
/** * nums的所有元素,假设最大能连续形成[1,sum] 当增加一个element的时候 * 会变成 [1,sum] [element+1,sum+element]两个区间,这两个区间有以下可 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode题目按公司分类
LinkedIn(39) 1 Two Sum 23.0% Easy 21 Merge Two Sorted Lists 35.4% Easy 23 Merge k Sorted Lists 23.3% ...
随机推荐
- SD卡的控制方法(指令集和控制时序)
1.SD卡的命令格式: SD卡的指令由6字节(Byte)组成,如下: Byte1:0 1 x x x x x x(命令号,由指令标志定义,如CMD39为100111即16进制0x27,那么完整的CMD ...
- ural1424 Minibus
Minibus Time limit: 1.0 secondMemory limit: 64 MB Background Minibus driver Sergey A. Greedson has b ...
- 转:WebDriver(Selenium2)模拟鼠标经过事件
在自动化测试过程中,由于javascript的使用,我们常常需要点击一些鼠标经过显示的菜单等元素,这时需要触发该元素的鼠标经过事件.使用WebDriver有以下两种实现. 1.使用Action pub ...
- PAT (Advanced Level) 1048. Find Coins (25)
先对序列排序,然后枚举较小值,二分较大值. #include<iostream> #include<cstring> #include<cmath> #includ ...
- Client与Server
监听套接字,通讯套接字,初始化网卡,多线程.想查自己的IP,ipconfig 服务端 #include "stdafx.h" #include <WinSock2.h> ...
- NOI2004 郁闷的出纳员 Splay
郁闷的出纳员 [问题描述] OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常, ...
- 杀死进程kill和fuser
1 kill -9 id 2 不行的话使用 fuser -k -SIGHUP /opt/bre/cookiemapping/wsapi/123 fuser有一个特别的用法在于它可以一次杀死那 ...
- Struts2不配置result参数 进行跳转实现
的 this.getRequest().getRequestDispatcher("url").forward(this.getRequest(),this.getResponse ...
- tls session resumption
http://stackoverflow.com/questions/12318325/resume-tls-connection-in-java As long as you use the sam ...
- Tomcat 静态部署 二步特别注意
一.修改server.xml 在Host 节点添加如下配置 <!-- path 为请求url地址 docBase 为项目文件绝对地址制定到WebContent根目录下 --> <Co ...