问题描述:

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的更多相关文章

  1. 330. Patching Array

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

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

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

  3. [LeetCode] Patching Array 补丁数组

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

  4. Patching Array

    引用原文:http://blog.csdn.net/murmured/article/details/50596403 但感觉原作者的解释中存在一些错误,这里加了一些自己的理解 Given a sor ...

  5. LeetCode Patching Array

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

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

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

  7. LeetCode-330.Patching Array

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

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

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

  9. LeetCode题目按公司分类

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

随机推荐

  1. Linux Shell编程(30)——别名

    Bash别名本质上是一个简称, 缩写, 这可避免键入过长的命令序列. 例如,如果我们添加 alias lm="ls -l | more" 这一行到文件~/.bashrc file里 ...

  2. Linux下的各种软件安装方法汇总

    1 RPM包 1.1 安装 RPM包就像Windows的EXE安装文件一样,各种文件都已经编译好了,并进行了打包,哪一个文件应该放在哪一个目录下都指定好了,安装非常方便,在图形界面里你只需要双击就能自 ...

  3. 【索引】Objective-C基础教程-读书笔记

    第1章 启程 http://www.cnblogs.com/duxiuxing/p/5492219.html 第2章 对C的扩展 第3章  面向对象编程的基础知识 第4章 继承 第5章 复合 第6章 ...

  4. sql2005中如何启用SA账号

    如下图

  5. 《算法问题实战策略》-chaper15-计算几何-线段相交

    这篇文章着力来讨论线段相交这一个问题. 给出两条线段,如何判断这两条线段相交? 如果这两条线段相交,如何求其交点? 线段相交问题通常由于其繁杂的情况种类而让人避而远之,在这里希望通过笔者的简化讨论希望 ...

  6. Node.js学习(12)----Web应用开发

    1.使用http模块 Node.js 由于不需要另外的 HTTP 服务器,因此减少了一层抽象,给性能带来不少提升, 但同时也因此而提高了开发难度.举例来说,我们要实现一个 POST 数据的表单,例如: ...

  7. 【转】CPU调度

    转自:http://blog.csdn.net/xiazdong/article/details/6280345 CPU调度   用于多道程序 以下先讨论对于单CPU的调度问题. 回顾多道程序,同时把 ...

  8. 推荐一个网站——聚合了微软的文件的Knowledge Base下载地址

    Microsoft Files是一个微软的文件数据库,从这里可以很方便的找到各个文件版本对应的下载链接. 比如今天debug需要找一个特定版本的sos.dll,从这个网站就很方便的给出了这个sos.d ...

  9. 【Android - MD】之RecyclerView的使用

    RecyclerView是Android 5.0新特性--Material Design中的一个控件,它将ListView.GridView整合到一起,可以使用极少的代码在ListView.GridV ...

  10. [Oracle] Data Pump 详细使用教程(4)- network_link

    [Oracle] Data Pump 详细使用教程(1)- 总览 [Oracle] Data Pump 详细使用教程(2)- 总览 [Oracle] Data Pump 详细使用教程(3)- 总览 [ ...