LeetCode-330.Patching Array
/**
* 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的更多相关文章
- [LeetCode] 330. Patching Array 数组补丁
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- 330. Patching Array
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- 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 ...
- [LeetCode] Patching Array 补丁数组
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- LeetCode Patching Array
原题链接在这里:https://leetcode.com/problems/patching-array/ 题目: Given a sorted positive integer array nums ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- [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( ...
- [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 ...
- [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. 这道 ...
- [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 ...
随机推荐
- ubuntu16.04+pycharm+默认文件头注释
安装 1.sudo gedit /etc/hosts 2.最后一行添加 0.0.0.0 account.jetbrains.com 3.从 http://idea.lanyus.com/ 中获取激活码 ...
- BETA-2
前言 我们居然又冲刺了·二 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 过去两天完成了哪些任务 了解OpenCV下的视频参数及其调用方法 初步编码 接下来的计划 文档工作 速 ...
- week3b:个人博客作业
vs2013的初步使用 由于自己在写这篇博客的时候已经把vs2013安装完毕,就我写了. 这是2013的使用步骤 第一步 注释 箭头指的方向是“点击” 第二步 第三步 第四步 第5步 第六步 最后点击 ...
- 深入理解Java类加载器(2)
1 基本信息 每个开发人员对Java.lang.ClassNotFoundExcetpion这个异常肯定都不陌生,这背后就涉及到了java技术体系中的类加载.Java的类加载机制是技术体系中比较核心的 ...
- 360Vedio To NFOV Vedio
Deep 360 Pilot Learning a Deep Agent for Piloting through 360° Sports Videos 源码.数据集和视频演示 ego-centric ...
- 用vue实现省市县三级联动
我真的没想到这个会困扰到我.最开始以为,不就是直接找个简单的插件就实现了吗,jquery插件找了几个,都没有达到目的. 需求是这样的: 点击input框,弹出一个popup,然后可以滚动选择省,市,县 ...
- 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC" task.
在安装一个从XE6复制到XE4的控件时出现编译错误: [MSBuild Error] "0" is an invalid value for the "DebugInfo ...
- 用delphi开发activex打印控件
http://blog.csdn.net/baronyang/article/details/4465468
- jndi连接数据库配置过程总结
一.我们先找到tomcat安装目录中conf目录下的context.xml更改里面的内容: <?xml version='1.0' encoding='utf-8'?> <Conte ...
- [转帖] SQLNET.ORA的处理.
被一个客户端连接远程数据库阻塞超时的问题困扰了好久,最后终于找到了答案 https://blog.csdn.net/herobox/article/details/16985097 Oracle ...