Patching Array
引用原文:http://blog.csdn.net/murmured/article/details/50596403
但感觉原作者的解释中存在一些错误,这里加了一些自己的理解
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.
该题的解题思路来源于网络,实际上是一个动态规划的算法题,不过规律稍微有点不好找,规律如下:
设:sum为当前所能达到的最大的连续和,并已有已知的一个数字集合M,这个时候在集合M中插入一个数字num,有一下情景
1:当num<=sum+1时,sum可以得到扩展sum=sum+num;
2:当num>sum+1时,此时sum不可以直接扩展,因为此时由sum+num的到的范围并不是连续的,如{1,2,3} ,sum=6,如果此时插入一个数字8,集合变为{1,2,3,8}这个时候是得不到7这个和的,所以只能再插入一个数字,这个时候为了尽可能增加sum,并保持连续性,最好的选择是插入sum+1这个数字,插入后更新sum=sum*2+1;然后再把num和sum+1进行比较,进行相应的处理,直到sum达到所需要的值。
由此可知,我们需要记录sum+1的值,假设n为我们的预期,那么当sum+1=n时,我们仍然要继续计算,因为这个时候sum=n-1,并没有达到我们的预期
需要注意的点:集合中的数字是int型的,但是sum的值可能超出int的范围
Patching Array的更多相关文章
- [LeetCode] Patching Array 补丁数组
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- LeetCode Patching Array
原题链接在这里:https://leetcode.com/problems/patching-array/ 题目: Given a sorted positive integer array nums ...
- 330. Patching Array
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- [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 数组补丁
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% ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- Calendar
/* * Calendar:它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MONTH.HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方 ...
- java和h5 canvas德州扑克开发中(二)
德州扑克网页源码在github上分享 https://github.com/lxr1907/pokers 感兴趣的可以上去看下. 1.通讯使用websocket,主要在message.js中. 2.用 ...
- Java 反射 getDeclareFields getModifiers setAccessible(true)
示例代码: public static Map<String, Object> dtoToMap(Object obj, String pre, String las ...
- Mybatis获取插入记录的自增长ID(转)
1.在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名,而不是表格的字段名. <ins ...
- 强大的commons
在此之前对commons包了解的不多,最常用的就是StringUtils.isBlack(str)来取代if(str !=null && str.lenght()>1)的判断,昨 ...
- No.7__C#_Final
实习期的完结,暂时告一个段落了 现在是周五的晚上十点三刻,洗完澡后,决定先写一点.今天是实习的最后一天,平平淡淡的完成了实习作业.本以为周五会像往常那样迫不及待的离开,迎接自己的 周末.可是,今天下午 ...
- .Net中的泛型(where T : class的含义)
Eg: class A<T>where T:new() where表明了对类型变量T的约束关系.where T: A表示类型变量是继承于A的,或者是A本身.where T:new()指明了 ...
- ANSYS经典APDL编程
在使用ANSYS的过程中的一些经验总结: Ansys Workbench 有限元分析虽然进入UI阶段,但是语言命令仍然是其基础核心. 1.ANSYS中的一些关键概念的理解; 参数化程序设计语言(APD ...
- Java面向对象㈡ -- 继承与多态
Java的继承是通过extends和implement来实现的,Java不支持多继承,但是Java支持多层继承以及多实现(接口).Java继承有一个关键字super是用来指向父类.Java继承衍生出覆 ...
- 更新程序基于IIS
闲着无聊写了个更新程序; 1.支持多种文件格式 2.支持按版本号/文件大小/文件时间进行判断更新 3.支持多级文件目录创建