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 ...
随机推荐
- 第二阶段每日站立会议First Day
昨天我进行了用户界面的修改,例如按钮的大小,位置,使界面看起来更美观.更简洁 今天准备安装在手机端进行界面效果测试以及进一步完善 遇到的问题:有些按钮由于在之前固定好的布局之中,所以没法移动其位置
- 福大软工1816 · 评分结果 · beta冲刺总评
作业地址:beta答辩总结.beta冲刺7.beta冲刺6.beta冲刺5.beta冲刺4.beta冲刺3.beta冲刺2.beta冲刺1.beta冲刺前准备 作业提交准则 按时交 - 有分 晚交 - ...
- 福大软工1816 · 评分结果 · Alpha冲刺答辩总结
作业地址:https://edu.cnblogs.com/campus/fzu/Grade2016SE/homework/2462 作业提交准则 按时交 - 有分 晚交 - 0分 迟交一周以上 - 倒 ...
- 如何在mvc项目中使用apiController
文章地址:How do you route from an MVC project to an MVC ApiController in another project? 文章地址:How to Us ...
- SQL语句中的output用法
private void button2_Click(object sender, RoutedEventArgs e) { using (SqlConnection conn = new SqlCo ...
- angularJS1笔记-(12)-自定义指令(compile/link)
index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- windows下的C++ socket服务器(1)
windows下的一个C++ socket服务器,用到了C++11的相关内容,现在还不是很完善,以后会不断改进的! #include <winsock2.h>//1 以后会用这种方式对特定 ...
- jQ 小球碰撞检测
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 性能分析_linux服务器CPU_Load Average
CPU度量Load Average 1. 概念介绍 1.1 Linux系统进程状态 在linux中,process有以下状态: runnable (就绪状态):blocked waiting fo ...
- pygame学习笔记(4)——声音
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi pygame.mixer是一个用来处理声音的模块,其含义为“混音器”.游戏中对声音的处理一般包括制造声音和播放声音 ...