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% ...
随机推荐
- 利用transform制作幻灯片
html代码 <html><head></head><body><div class='hpic'> <ul style=" ...
- 扩展欧几里得 POJ 1061
感觉这道题目的数据好水啊...我的代码我都觉得姿势特别奇怪...竟然还过了... 好吧,原来不是姿势奇怪,而是逆元需要用的时候是余数也需要的时候,这里的余数是不需要的,所以就AC了 就说一下碰到的问题 ...
- iptables配置vsftp访问
一.FTP服务简介 FTP服务器有两种工作模式:主动模式和被动模式.这两种方式的特点如下: (1)主动模式下: tcp, 20(20号端口用于数据传输),21(21号端口用于控制连接) (2 ...
- ASP.NET 的内置对象
ASP.NET的内置对象介绍 1.Response 2.Request 3.Server 4.Application 5.Session 6.Cookie Request对象主要是让服务器取得客户端浏 ...
- HDU - 2680 最短路 spfa 模板
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...
- 一个action读取另一个action里的session
action 1: private Map session; session.put("projectname_session", request1.getParameter(&q ...
- 在Delphi中使用C++对象(转)
源:http://blog.csdn.net/henreash/article/details/7352335 Delphi是市场上最好的RAD工具,但是现在C++占据着主导地位,有时针对一个问题很难 ...
- HDU 5611 Baby Ming and phone number
#include<cstdio> #include<cstring> #include<vector> #include<cmath> #include ...
- 浅谈mysql主从复制的高可用解决方案
1.熟悉几个组件(部分摘自网络)1.1.drbd —— DRBD(Distributed Replicated Block Device),DRBD号称是 "网络 RAID" ...
- (简单) POJ 2502 Subway,Dijkstra。
Description You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of ...