/**
* nums的所有元素,假设最大能连续形成[1,sum] 当增加一个element的时候
* 会变成 [1,sum] [element+1,sum+element]两个区间,这两个区间有以下可能性:
* 1 相交: element < sum
* 2 连续: element = sum 是保持连续,并使得加的元素最少的最大值
* 3 相离: element > sum 大于sum就不连续了
* */
private int minPatches(int[] nums, int n) {
long sum = 1;//1 是必需得有,因为要形成[1,n]的分布,肯定得有1,如果nums没有,就要把其加上
int index= 0;
int add = 0;
while(sum <= n ){
if(index < nums.length && nums[index] <=sum){
sum += nums[index];
index++;
}
else{
sum += sum;
add++;
}
}
return add;
}

LeetCode-330.Patching Array的更多相关文章

  1. [LeetCode] 330. Patching Array 数组补丁

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

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

  4. [LeetCode] Patching Array 补丁数组

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

  5. LeetCode Patching Array

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

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

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

  7. [LeetCode] Sort Transformed Array 变换数组排序

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  8. [LeetCode] Product of Array Except Self 除本身之外的数组之积

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  9. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  10. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

随机推荐

  1. 内网php项目访问(切换在线解决)

    之前内网访问出现过问题: 可参考手机访问本地php项目遇到的问题及解决(2015-06-20 09:41) 后来重装wamp之后,要访问还是出现问题 即http://192.168.191.1/mui ...

  2. printf in KEIL C51

    转自:http://blog.csdn.net/it1988888/article/details/8821713 在keil中printf默认是向串口中发送数据的,所以,如果应用该函数,必须先初始化 ...

  3. SQL Server学习记录之获取每月每季度每年第一天和最后一天

    DECLARE@dtdatetime SET@dt=GETDATE() DECLARE@numberint --1.指定日期该年的第一天或最后一天 --A. 年的第一天 SELECTCONVERT() ...

  4. SCRIPT7002: XMLHttpRequest: 网络错误 0x2efe, 由于出现错误 00002efe 而导致此项操作无法完成

    google中带中文参数可能查询,但是在IE带中文参数不能查询:报如下错误 SCRIPT7002: XMLHttpRequest: 网络错误 0x2efe, 由于出现错误 00002efe 而导致此项 ...

  5. 内网内使用https 和 使用http 建立连接的速度对比

    文字版 使用https httpstat https://10.24.101.14/cwbase/web/Login.aspx --insecure Connected to HTTP/ OK Ser ...

  6. object & over-write

    object & over-write

  7. UVA11324_The Largest Clique

    极大团.即求一个最大点集,使得点集中的任意两个点u,v至少存在u->v,或者v->u的路径. 是这样做的,求出所有的联通分量,然后整个图就变成了无环图,把原来若干个点缩点,点权为分量的点数 ...

  8. Codechef_JULY14

    感觉这套比赛题目比较容易,没有以前做过的某次codechef那么凶残.题目还是很有意思的,最好的是有中文翻译. CSUB:签到题,直接从左往右扫一遍即可,维护前面出现过多少个1. #include & ...

  9. FlatBuffers初探

    我第一次知道FlatBuffers是因为Facebook写的这篇Android的技术博客文章.它主要介绍了FlatBuffers对比JSON的优势,以及Facebook Android App应用了F ...

  10. BZOJ5102 POI2018Prawnicy(堆)

    考虑固定右端点,使左端点最小.那么按右端点排序后查询前缀这些区间的左端点第k小即可.然而写了一个treap一个线段树都T飞了,感觉惨爆.事实上可以用堆求第k小,维护一个大根堆保证堆中元素不超过k个即可 ...