LeetCode-Pathcing 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
.
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的更多相关文章
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Patching Array 补丁数组
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- [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 ...
- LeetCode Patching Array
原题链接在这里:https://leetcode.com/problems/patching-array/ 题目: Given a sorted positive integer array nums ...
- Leetcode: Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 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 ...
- [LeetCode] Circular Array Loop 环形数组循环
You are given an array of positive and negative integers. If a number n at an index is positive, the ...
- [LeetCode] Non-decreasing Array 非递减数列
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
随机推荐
- swift向方法传数组参数的语法
总是记不住向方法中传数组参数的语法,所以记录一下. func calculateStatistics(scores:[Int]) -> (min:Int,max:Int,sum:Int) { v ...
- VS2012安装git
新安装了vs2012,尝试使用Git,安装过程比较简单,记录下来以备以后查阅. 1. 下载.安装Git 我的系统是Windows 7,需要安装Git for Windows. 下载地址: http ...
- python selenium --处理下拉框
下拉框是我们最常见的一种页面元素,对于一般的元素,我们只需要一次就定位,但下拉框里的内容需要进行两次定位,先定位到下拉框,再定位到下拉框内里的选项. drop_down.html <html&g ...
- java 虚函数
猜猜这里的代码输出的结果是多少? package test; public class ConstructorExample { static class Foo { int i; Foo() { i ...
- python \uxxxx转中文,Python列表中的字典 \uxxxx转中文,
import json a = [{u', u'roleFlag': 7}] print json.dumps(a).decode("unicode_escape") 输出结果是: ...
- hdu 1711 Number Sequence KMP 基础题
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 利用putty的pscp命令上传文件
1.有的时候,本地下载源码包的速度快过vps(主要指国内),那么可以用迅雷下载后上传到vps. 2.为了这么点事情,专门安装一个ftp软件,太麻烦,于是想到了putty贴心的pscp小程序. 3.首先 ...
- Atitit .h5文件上传
Atitit .h5文件上传 1. 上传原理1 2. Html1 3. Js2 4. uploadV2.js2 5. upServlet & FileUploadService {3 6. 注 ...
- ManipulationStarted,ManipulationCompleted,ManipulationDelta
一.获取某个元素相对另一元素的相对位置 1.使用TransformToVisual获取某个元素相对于另外一个元素的偏移量. 例如:要获得rect相对于LayoutRoot的偏移量,就将LayoutRo ...
- 完整学习使用CSS动画【翻译】
注:原文有较大改动 使用keyframes, animation属性,例如timing, delay, play state, animation-count, iteration count, d ...