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.

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

Analysis:

Very clear analysis from https://discuss.leetcode.com/topic/35494/solution-explanation

Explanation

Let miss be the smallest sum in [0,n] that we might be missing. Meaning we already know we can build all sums in [0,miss). Then if we have a number num <= miss in the given array, we can add it to those smaller sums to build all sums in [0,miss+num). If we don't, then we must add such a number to the array, and it's best to add miss itself, to maximize the reach.


Example: Let's say the input is nums = [1, 2, 4, 13, 43] and n = 100. We need to ensure that all sums in the range [1,100] are possible.

Using the given numbers 1, 2 and 4, we can already build all sums from 0 to 7, i.e., the range [0,8). But we can't build the sum 8, and the next given number (13) is too large. So we insert 8 into the array. Then we can build all sums in [0,16).

Do we need to insert 16 into the array? No! We can already build the sum 3, and adding the given 13 gives us sum 16. We can also add the 13 to the other sums, extending our range to [0,29).

And so on. The given 43 is too large to help with sum 29, so we must insert 29 into our array. This extends our range to [0,58). But then the 43 becomes useful and expands our range to [0,101). At which point we're done.

-------------------------------------------------------------------------

Solution:

 public class Solution {
public int minPatches(int[] nums, int n) {
long nextMiss = 1;
int ind = 0, added = 0;
while (nextMiss <= n){
if (ind < nums.length && nums[ind] <= nextMiss){
nextMiss += nums[ind++];
} else {
nextMiss += nextMiss;
added++;
}
}
return added;
}
}

LeetCode-Pathcing Array的更多相关文章

  1. C++ STL@ list 应用 (leetcode: Rotate Array)

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

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

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

  3. [LeetCode] Patching Array 补丁数组

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

  4. [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  ...

  5. LeetCode Patching Array

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

  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 ...

  10. [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

随机推荐

  1. 重置linux mysql root密码

    2.修改MySQL的登录设置: # vi /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] datadir=/var/lib/my ...

  2. wpf 帧动画

    帧动画实现很简单 <ImageBrush x:Key="speed_s" Stretch="Fill" ImageSource="/images ...

  3. atitit.系统托盘图标的设计java swing c# .net c++ js

    atitit.系统托盘图标的实现java swing c# .net c++ js 1. 系统托盘图标的结构 1 2. Java swing的实现 1 3. .net的实现 1 4. C++的实现 1 ...

  4. __attribute__机制介绍(转)

    转自 http://blog.csdn.net/ithomer/article/details/6566739 1. __attribute__ GNU C的一大特色(却不被初学者所知)就是__att ...

  5. Python unittest 参数化

    准备工作: pip install  nose_parameterized 典型场景:用户名.密码参数化 实例 1,新建一个ftl.py 文件 ,用来将存在于.txt .xlsx 文件中的参数化数据转 ...

  6. WEEX快速入门

    WEEX快速入门 WEEX 是阿里推送的一款基于Node.js,轻量级的移动端跨平台动态性技术解决方案,用于构建原生的速度的跨平台APP. 1. 搭建WEEX环境 1.1 首先下载安装Node.js, ...

  7. Struts提交form之后抛出异常java.lang.IllegalArgumentException: The path of an ForwardConfig cannot be null

    原因:在ActionForm中使用了ActionErrors,并且ActionErrors中的内容不为空,所以Struts会根据action的配置跳转到input指定的页面.但是我在配置action的 ...

  8. TCP/IP详解读书笔记:链路层

    在TCP/IP协议族中,链路层主要有三个目的: 1)为IP模块发送和接受IP数据报: 2)为ARP模块发送ARP请求和接受ARP应答: 3)为RARP模块发送RARP请求和接受RARP应答: 以太网和 ...

  9. Golang并发原理及GPM调度策略(一)

    其实从一开始了解到go的goroutine概念就应该想到,其实go应该就是在内核级线程的基础上做了一层逻辑上的虚拟线程(用户级线程)+ 线程调度系统,如此分析以后,goroutine也就不再那么神秘了 ...

  10. Docker是用来干什么的?【快速入门】

    Docker从去年开始不仅能在Linux下运行 ,还支持windows.osX等主流系统. 下面的例子我自己经常使用,当然你有更好的案例也可以分享给我. 尝试新软件 对开发者而言,每天会催生出的各式各 ...