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.

Java:

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){
miss = miss + nums[i];
i++;
}else{
miss += miss;
count++;
}
} return count;
} 

Python:

class Solution(object):
def minPatches(self, nums, n):
"""
:type nums: List[int]
:type n: int
:rtype: int
"""
patch, miss, i = 0, 1, 0
while miss <= n:
if i < len(nums) and nums[i] <= miss:
miss += nums[i]
i += 1
else:
miss += miss
patch += 1 return patch  

C++:

class Solution {
public:
int minPatches(vector<int>& nums, int n) {
long miss = 1, res = 0, i = 0;
while (miss <= n) {
if (i < nums.size() && nums[i] <= miss) {
miss += nums[i++];
} else {
miss += miss;
++res;
}
}
return res;
}
};

C++:

class Solution {
public:
int minPatches(vector<int>& nums, int n) {
long miss = 1, k = nums.size(), i = 0;
while (miss <= n) {
if (i >= nums.size() || nums[i] > miss) {
nums.insert(nums.begin() + i, miss);
}
miss += nums[i++];
}
return nums.size() - k;
}
};

  

All LeetCode Questions List 题目汇总

[LeetCode] 330. Patching Array 数组补丁的更多相关文章

  1. [LeetCode] Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  2. 330. Patching Array

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

  3. 【LeetCode】1095. 山脉数组中查找目标值 Find in Mountain Array

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetco ...

  4. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  5. 了解PHP中的Array数组和foreach

    1. 了解数组 PHP 中的数组实际上是一个有序映射.映射是一种把 values 关联到 keys 的类型.详细的解释可参见:PHP.net中的Array数组    . 2.例子:一般的数组 这里,我 ...

  6. JavaScript的json和Array及Array数组的使用方法

    1.关于json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集.也可以称为数据集和数组类似,能够存数据! //Ar ...

  7. iOS -Swift 3.0 -Array(数组与可变数组相关属性及用法)

    // // ViewController.swift // Swift-Array // // Created by luorende on 16/9/12. // Copyright © 2016年 ...

  8. 多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量

    多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量,并且数组变量可以直接取到每一个元素var array1 = '<%=yearList =>'; ...

  9. c++中的array数组和vector数组

    我觉得实验一下会记得比较牢,话不多直接上代码. 下面是array数组,感觉用的不多. //cpp 风格数组 array #include <iostream> #include <a ...

随机推荐

  1. unicode转换为中文

    unicode转换为中文 \u5f53\u5730\u65f6\u95f42019\u5e747\u670813\u65e5\uff0c\u82f1\u56fd\u8d1d\u5fb7\u798f\u ...

  2. js数组操作 求最大值,最小值,正序、倒叙大小值排序,去重复

    var arr = [1,5,2,56,12,34,21,3,5] Math.min.apply({},arr) Math.max.apply({},arr) arr.sort((m,n)=>m ...

  3. python的readline() 和readlines()

    .readline() 和 .readlines() 之间的差异是后者一次读取整个文件,象 .read() 一样..readlines() 自动将文件内容分析成一个行的列表,该列表可以由 Python ...

  4. iptables的使用

    四表五链 四表(table):raw.mangle.nat.filter 五链(chain):PREROUTING.INPUT.FORWARD.OUTPUT.POSTROUTING 每个表存在几个或全 ...

  5. [Dart] Mixin

    Docs Mixins are a way of reusing a class’s code in multiple class hierarchies. void main() { Animal ...

  6. [NPM + React] Prepare a Custom React Hook to be Published as an npm Package

    Before we publish our package, we want to make sure everything is set up correctly. We’ll cover vers ...

  7. Tensorflow细节-P309-监控指标可视化

    注意下面一个点就ok了 with tf.name_scope('input_reshape'): # 注意看这里,图片的生成 image_shaped_input = tf.reshape(x, [- ...

  8. window对象方法(open和close)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. BZOJ 4212: 神牛的养成计划 可持久化trie+trie

    思路倒是不难,但是这题卡常啊 ~ code: #include <bits/stdc++.h> #define N 2000004 #define M 1000005 #define SI ...

  10. dinoql 支持自定义resovler了

    dinoql 当前版本0.4.0 支持自定义reovler 了,使用也比较简单 环境准备 初始化 yarn init -y 添加依赖 yarn add dinoql graphql-tag packa ...