原题链接在这里:https://leetcode.com/problems/patching-array/

题目:

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.

题解:

定义一个miss, 表示[0,n]中目前最小不能表示的数,初始为1,为啥不为0呢,因为n=0没啥意义,直接返回0了。那么此时我们能表示的范围是[0, miss), 表示此时我们能表示0到miss-1的数.

如果此时的num[i] <= miss,那么我们可以把我们能表示数的范围扩大到[0, miss+num[i]), i++; 如果num[i]>miss, 那么此时我们需要添加一个数,为了能最大限度的增加表示数范围, miss翻倍.

给定nums = [1, 2, 4, 11, 30], n = 50. 我们需要让[0, 50]之间所有的数字都能被nums中的数字之和表示出来。

首先使用1, 2, 4可能表示出0到7之间的所有数,表示范围为[0, 8), 但我们不能表示8, 因为下一个数字11太大了, 所以我们要在数组里加上一个8, 此时能表示的范围是[0, 16), 那么我们需要插入16吗,答案是不需要,因为我们数组有1和4, 可以组成5, 而下一个数字11, 加一起能组成16, 所以有了数组中的11, 我们此时能表示的范围扩大到[0, 27), 但我们没法表示27, 因为30太大了,所以此时我们给数组中加入一个27, 那么现在能表示的范围是[0, 54), 已经满足要求了,我们总共添加了两个数8和27,所以返回2即可。

若是空数组{}, n = 7. 此时要返回3, 因为添加了三个数1,2,4.

Time Complexity: O(max(logn, nums.length)). n 是[0,n]的范围上线. logn是因为一次翻两倍. 几次能翻到n. Space: O(1).

AC Java:

 public class Solution {
public int minPatches(int[] nums, int n) {
long miss = 1;
int count = 0;
int i = 0;
while(miss <= n){
if(i < nums.length && nums[i] <= miss){ //若是nums[i] <= miss, 就把miss提高到miss+nums[i]. i++
miss += nums[i++];
}else{ //若是nums[i]>miss, 此时需要加一个数, miss翻倍, i不动.
miss <<= 1;
count++;
}
}
return count;
}
}

Reference: https://leetcode.com/discuss/82822/solution-explanation

http://www.cnblogs.com/grandyang/p/5165821.html

LeetCode 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. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

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

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

  4. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  5. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  6. Leetcode: Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  7. LeetCode 561. Array Partition I (数组分隔之一)

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  8. [LeetCode] Circular Array Loop 环形数组循环

    You are given an array of positive and negative integers. If a number n at an index is positive, the ...

  9. [LeetCode] Non-decreasing Array 非递减数列

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

随机推荐

  1. 利用ngxtop实时监控nginx的访问情况

    关于对nginx web server的实时访问的实时监控问题,我很久之前就想实现的,现在虽有nginx自带的status扩展,但那是全局的,无法细分到vhost,并且提供的metric也很少,加之目 ...

  2. Codeforces Round #235 (Div. 2)C、Team

    #include <iostream> #include <algorithm> using namespace std; int main(){ int n,m; cin & ...

  3. 【BZOJ】2697: 特技飞行

    题意 \(k(1 \le k \le 300)\)种物品,价值分别为\(c_i(0 \le c_i \le 1000)\).有\(n(1 \le n \le 1000)\)分钟,每分钟可以选择一个物品 ...

  4. 【Oracle】同义词问题

    优点: 1.节省数据库空间,多用户可以操作同一张表: 2.扩展的数据库的使用范围,能够在不同的数据库用户之间实现无缝交互: 3.利用Database Link.创建同义词可以实现不同数据库服务器之间的 ...

  5. 创建需要计时器的windows service

    1.在VS中建立windows service后,应该添加一个安装程序. 2.在默认的Service1.cs设计界面右键,添加安装程序,生成ProjectInstaller.包含两个类serviceP ...

  6. 获取jQuery对象的第N个DOM元素 && table常用css样式

    获取jQuery对象的第N个DOM元素 1.$(selector).get(N-1) 2.$(selector)[N-1] 注意:.index()方法返回的是一个数,相当于C#中的IndexOf() ...

  7. Java_关于App class loader的总结

    Java本身是一种设计的非常简单,非常精巧的语言,所以Java背后的原理也很简单,归结起来就是两点: 1.JVM的内存管理 理解了这一点,所有和对象相关的问题统统都能解决 2.JVM Class Lo ...

  8. c++ map 的基本操作

    Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数:   map<stri ...

  9. 手机横屏竖屏css

    @media是css3中新定义的,功能非常强大,顾名思义PC是无法匹配横竖屏的,所以orientation只对移动设备起效. 1.头部声明 复制代码 代码如下: <meta name=" ...

  10. javascript平时小例子③(setInterval使用1)

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...