引用原文: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的更多相关文章

  1. [LeetCode] 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

    原题链接在这里:https://leetcode.com/problems/patching-array/ 题目: Given a sorted positive integer array nums ...

  3. 330. Patching Array

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  4. [Swift]LeetCode330. 按要求补齐数组 | Patching Array

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  5. [LeetCode] 330. Patching Array 数组补丁

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  6. LeetCode-330.Patching Array

    /** * nums的所有元素,假设最大能连续形成[1,sum] 当增加一个element的时候 * 会变成 [1,sum] [element+1,sum+element]两个区间,这两个区间有以下可 ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. LeetCode题目按公司分类

    LinkedIn(39) 1 Two Sum 23.0% Easy 21 Merge Two Sorted Lists 35.4% Easy 23 Merge k Sorted Lists 23.3% ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. hdu3294 girl‘s research

    题目大意:有多组数据,每组数据给出一个字符和一个字符串.该字符将变成’a‘,表示字符串中的所有该字符将变成’a‘,同时其他字符也将做相同的偏移.具体来说,如果该字符为’b‘,表示字符串中的’b‘都将变 ...

  2. 黄聪:路由器WIFI连接无法正常访问个别网站及发送图片

    打开路由,路由默认MTU是1500,改成1472 就解决了

  3. centos6.5 安装mysql 的过程

    Linux中使用最广泛的数据库就是MySQL,使用在线yum的方式安装的版本落后MySQL网站好几个小版本, 所以折腾了几个星期,终于在CentOS 装好了mysql5.6,装完之后,对整个linux ...

  4. javascript的一点学习

    最近用vue.js用的很爽,在全栈开发的路上一路狂奔,发现后台跟前台一起确实更有意义. 记录一个比较有意思的bug: 目标是对一个全局的paramList进行json格式化显示.代码借鉴了 http: ...

  5. Unity UI on the HoloLens

    Following the steps under "Required configuration" will allow Unity UI to continue to work ...

  6. Solr导入MySql中的数据

    1.参照 http://www.cnblogs.com/luxh/p/5016894.html 部署好solr的环境 2.在solr_home下建立一个core_item目录 [root@iZ23ex ...

  7. mac 下打开多个Eclipse

    在Mac下只能打开一个Eclipse工具. 使用下面命令,在控制台中输入,可以打开多个Eclipse. open -n xx/xx/eclipse.app 例子: open -n /Users/use ...

  8. SQL归档

    /* 作用:归档3个月前的短信发送记录 创建人:zhuxiang 日期:2012-10-30 */ ALTER Proc [dbo].[Sms_Job_History_Send] As Begin D ...

  9. SQL Server备份脚本

    declare @bakfile varchar(30), @bakfilediff varchar(30),@pathfull varchar(50),@pathdiff varchar(50)se ...

  10. php SimpleXML 例子

    $txt = GetRemoteText($url); if(strlen($txt) > 0) { $xml = simplexml_load_string($txt); //获取xml if ...