引用原文: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. CodeSmith模板

    重复性的工作交给代码生成器就好,这里分享几套模板, 1.从数据库抓取字段,生成Table元素,这个在web开发中很有用 <%-- Name: Author: Description: --%&g ...

  2. Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体旋转)

    Manipulation gesture:保持点击手势,在3D世界中绝对运动 当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动.缩放或旋转全息图像.如此的一个用处是使得用户可以在世界中绘 ...

  3. 《前端们,贺老 Live 面试你了!》所引发的思考与实践

    贺老在知乎live中提到了一个这样的问题: 产品经理提出了一个需求:用户点击文章阅读,返回之后阅读其他文章.当用户看得多了,容易点到自己看过的文章,造成时间浪费.所以想给点击过的文章加一个标记,如:& ...

  4. thinkphp关闭调试模式(APP_DEBUG => false),导致程序出错

    thinkphp关闭调试模式(APP_DEBUG => false),导致程序出错,开启调试模式,不报错,怎么解决? 查看Logs日志记录: [ --29T09::+: ] 113.108.11 ...

  5. InfoPath中用户数据类型结构解析

    由于缺少对“用户”数据类型结构的明确定义,在设计InfoPath表达式时往往会耗用大量时间尝试,而且不一定能得到期望结果.下面对用户数据类型的结构进行详细解析. >> 什么是用户数据类型 ...

  6. 初学PHP

    这东西必须得静下心来学,快是快不来的,得有一个痛苦的过程.<PHP和MySQL WEB开发>这本书很值得一看,有了坚实的基础,推荐看<深入php++面向对象.模式与实践+第三版> ...

  7. OpenGL学习笔记4——模型视图变换

    以日月地为例的一个模型视图变换.绕了比较多的弯路,下面是几个注意点总结. 注意点: 1.GL函数对模型的操作是基于当前局部坐标系,即模型坐标系而非世界坐标系,二者只在第一次初始化完毕之后才重合: 2. ...

  8. js table的笔记,实现添加 td,实现搜索功能

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  9. django--模板(七)

    1.模板加载 一个项目可以有多个应用,每个应用下都可以有模板,思考模板的查找顺序?

  10. 进击的docker 一 : Docker 简介

    Docker简介 1.什么是docker 1.1.docker 起源 开源项目 诞生2013年初 GO语言开发实现 遵从了Apache2.0协议 项目代码在GitHub维护 1.2.docker目标 ...