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 ...
随机推荐
- doPost方法与doGet方法
例子我们发现forward跳转访问Servlet说不定的感觉,其实我们要想弄明白这个问题,就要从forward本身来研究了. 我们都知道 forward跳转是转发请求,不转发地址的,简单点说,forw ...
- js两个小技巧【看到了就记录一下】
1.不声明第三个变量实现交换 ,b=; a=[b,b=a][];//执行完这句代码之后 a的值为2 b的值为1了 2.&&和||的用法 (学会了立马感觉高大尚了吧) ; //传统if语 ...
- 51Nod 1002 数字三角形 Label:水水水 && 非学习区警告
一个高度为N的由正整数组成的三角形,从上走到下,求经过的数字和的最大值. 每次只能走到下一层相邻的数上,例如从第3层的6向下走,只能走到第4层的2或9上. 5 8 4 3 6 9 7 ...
- jQueryUI日期显示
<script type="text/javascript" src="js/My97DatePicker/WdatePicker.js">< ...
- 【HDU】1848 Fibonacci again and again
http://acm.hdu.edu.cn/showproblem.php?pid=1848 题意:同nim,3堆,每次取的为fib数,n<=1000 #include <cstdio&g ...
- 【BZOJ1901】Zju2112 Dynamic Rankings
Description 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是 ...
- iOS应用内付费(IAP)开发步骤列表
iOS应用内付费(IAP)开发步骤列表 前两天和服务端同事一起,完成了应用内付费(以下简称IAP, In app purchase)的开发工作.步骤繁多,在此把开发步骤列表整理如下.因为只是步骤列表, ...
- 20145330Java程序设计第三次实验
20145330<Java程序设计>第三次实验报告 实验三 敏捷开发与XP实践 实验内容 1.使用git上传代码 2.使用git实现代码开发实践 3.实现代码的重载 实验步骤 使用git上 ...
- Defining custom settings in Odoo
Unfortunately Odoo documentation doesn’t seem to include any information about adding new configurat ...
- [转载]对于GetBuffer() 与 ReleaseBuffer() 的一些分析
先 转载一段别人的文章 CString类的这几个函数, 一直在用, 但总感觉理解的不够透彻, 不时还有用错的现象. 今天抽时间和Nico一起分析了一下, 算是拨开了云雾: GetBuffer和Rele ...