LeetCode Patching Array
原题链接在这里: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的更多相关文章
- [LeetCode] Patching Array 补丁数组
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [LeetCode] 330. Patching Array 数组补丁
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- [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] 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: 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 ...
随机推荐
- 完数[HDU1406]
完数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- android NDK入门 windows下安装cygwin
一.Android NDK环境简介 Android NDK 是运行于Android 平台上的Native Development Kit 的缩写. Android 应用开发者可以通过NDK 调用C 或 ...
- 关于netstat
netstat 命令是 linux 下一个非常有用的命令,顾名思义,其作用是查询当前的网络状态. 以最常用的查询 TCP 连接状态为例 netstat -an|awk '/tcp/ {print $6 ...
- ACM: Gym 100935G Board Game - DFS暴力搜索
Board Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Gym 100 ...
- 转:关于垂直网格与CSS基线对其的探讨
网页设计布局中一直比较流行网格对齐,但只是针对水平的对齐,很少或者没有涉及垂直对齐,这篇文章很详细的讲解了垂直网格,乃至基线对其的相关,而css3中的多列布局的也使其显得更为重要,因此还是很有必要去了 ...
- 5分钟 wamp下php phpmaile发送qq邮件 2015最新方法说明
13:40 2015/11/20 5分钟 wamp下php phpmaile发送qq邮件 2015最新方法说明 关键点:现在qq邮箱开通smtp服务后会给你一个很长的独立新密码,发邮件配置中的密码需要 ...
- 自定义plain 样式的 tableview,模拟器上不显示分割线,真机上却显示分割线.
一, 经历 1> 自定义plain 样式的 tableview,模拟器上不显示分割线,真机上却显示cell 下面的分割线. 2> 尝试使用表格的separatorStyle属性,尝试失败. ...
- 使用SDWebImage下载图片,sharedDownloader方法下载成功,new 方法下载失败
一,经历 1.使用 new 方法创建下载对象时,下载图片总是失败,而且不会执行成功或失败后的回调. 2.参考别人的代码,用的是sharedDownloader来创建下载对象,可以顺利下载图片. 3.看 ...
- Func<T, TResult> Delegate
public delegate TResult Func<in T, out TResult>( T arg ) http://msdn.microsoft.com/en-us/libra ...
- #define is unsafe——I
I. #define is unsafe Have you used #define in C/C++ code like the code below? #include <stdio.h&g ...