引用原文: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. 每天一个 Linux 命令(16):which命令

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which  查看可执行文件的位置. whereis 查看文件的位置. locate   配合数据库查看文件位置 ...

  2. mybatis 中${}和#{}区别

    用#传入参数是,sql语句解析是会加上"",比如  select * from table where name = #{name} ,传入的name为小李,那么最后打印出来的就是 ...

  3. Laravel学习笔记(三)数据库 数据库迁移

    该章节内容翻译自<Database Migration using Laravel>,一切版权为原作者. 原作者:Stable Host, LLC 翻译作者:Bowen Huang 正文: ...

  4. 黑马程序员_ Objective-c 之block、protocol学习笔记

    一):block学习总结 block :用来保存一段代码. 1.block 特点:  1> Block封装了一段代码,可以在任何时候执行   2> Block可以作为函数或者函数的返回值, ...

  5. 在linux中配置安装telnet服务

    Telnet 是一种流行的用于通过 Internet 登录到远程计算机的协议.Telnet 服务器软件包为远程登录主机提供了支持.要通过 Telnet 协议与另一台主机通讯,您可以使用名称或 Inte ...

  6. pkg-config

    可以使用pkg-config获取的库需要有一个条件,那就是要求库的提供者,提供一个.pc文件.比如gtk+-2.0的pc文件内容如下: prefix=/usrexec_prefix=/usrlibdi ...

  7. jQuery中时间戳和日期的相互转换

    在项目中经常会使用时间戳和日期的相互转换,可以参考如下代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...

  8. C#调用 ICSharpCode.SharpZipLib.Zip 实现解压缩功能公用类

    最近想用个解压缩功能 从网上找了找 加自己修改,个人感觉还是比较好用的,直接上代码如下 using System; using System.Linq; using System.IO; using ...

  9. linux下RDP客户端及服务器

    tsclient redsktop remmina   -->对ubuntu支持的非常不错 XRdp 集合vnc作为rdp服务器端使用;

  10. iOS开发阶段技能总结

    这是一篇自己平时纪录的笔记... 1.基本的数据结构常识:链表,队列,栈 2.基本的算法:排序,动态规划等常用算法 3.基本的概念,cocoa,各种自带的view的使用. 4.xcode自带的测试:O ...