330. Patching Array--Avota
问题描述:
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.
题意:
给定非递减正数组nums和正整数n,问最少向数组中添加多少元素使得从nums[ ]中取若干元素的和能够覆盖[1, n]
解题思路:
我们可以试着从1到n检查每个数(记为current)是否满足。对于每一个current,先从输入数组nums中查看是否有满足条件的数(即是否nums[i] <= current,i表示nums数组中的数用到第几个,初始为0),若有则使用并进行i++、current += nums[i](current至current + nums - 1可由current-nums[i]到current - 1分别加上nums[i]得到)操作;若无则添加新元素current并进行current = current * 2操作。
具体的,扫描数组nums,更新原则如下:
- 若nums[i] <= current , 则把nums[i]用掉(即 i++),同时current更新为current + nums[i];
- 若nums[i] > current,则添加新的元素current,同时current更新为current * 2.
但须注意:
current从1开始
current可能超过int型,需使用long型
示例代码:
class Solution {
public:
int minPatches(vector<int>& nums, int n) {
int len = nums.size();
if(len == 0){
return log2(n) + 1;
}
long current = 1;
int i = 0, count = 0;
while(current <= n){
if(i < len && nums[i] <= current){
current += nums[i];
i++;
}
else{
count++;
current *= 2;
}
}
return count;
}
};
330. Patching Array--Avota的更多相关文章
- 330. 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 数组补丁
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 ...
- [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% ...
随机推荐
- Selenium API(C#)
1 Fetching a Page driver.Url = "http://www.google.com"; 2 Locating UI Elements (WebElement ...
- Unity3D学习笔记——选择Enemy
一.步骤: 1.创建三个Cube,并将这三个Cube的Cube的Tag设为Enemy 2.导入第一人称视角的资源 3.创建名为Targeting的C#脚本 4.编写Targeting脚本,并将它附到第 ...
- 【狼】unity3d iTween插件的学习
之前在一个三消游戏项目中接触到iTween,最近又要用到,发现iTween真的是一个很好用,省事的插件,有很多函数里包括 Hashtable的他都用一个函数很简单的完成了, 举几个例子 void Lo ...
- Hibernate(四)基本映射
映射的概念 在上次的博文Hibernate(三)Hibernate 配置文件我们已经讲解了一下 Hibernate中的两种配置文件,其中提到了两种配置文件的主要区别就是XML可以配置映射.这里提到的映 ...
- 使用devpartner的blockchecker检查c++内存错误
在仿写stl的过程中,被一处内存错误卡了很久.当内存池需要多次malloc时会出现堆损坏的错误,初步判断是数组越界,但总是检查不出来.一开始用Dr.Memory检查不出来,就试了一下devpartne ...
- Java GC专家系列2:Java 垃圾回收的监控
这是”成为GC专家系列”文章的第二篇.在第一篇理解Java垃圾回收中我们学习了几种不同的GC算法的处理过程,GC的工作方式,新生代与老年代的区别.到目前为止,你应该已经了解了JDK 7中的5种GC类型 ...
- Border - SGU 133(排序)
题目大意:有N对区间现在剔除一些区间,这些区间被另一些区间完全包含,如,Ai<Bi, Bj<Aj, A完全包含B,求出来这样被包含的区间个数. 分析:首先按照第一个数字先进行一下排序,然后 ...
- UIView与CALayer的区别,很详细
研 究Core Animation已经有段时间了,关于Core Animation,网上没什么好的介绍.苹果网站上有篇专门的总结性介绍,但是似乎原理性的东西不多,看得人云山雾罩,感觉,写那篇东西的人, ...
- wind安装selenium
1.按win+r进入运行模式,然后输入cmd进入命令行 2.输入pip按回车 查看命令帮助 3.然后输入pip install selenium 按回车,然后等待 下载,下载好了会自动安装完成 3.然 ...
- DB2单个DB重启
db2单个数据库重启 . -------------------------------------------------------------- db2 connect to bpm user ...