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 ...
随机推荐
- HDU 1170 Shopping Offers 离散+状态压缩+完全背包
题目链接: http://poj.org/problem?id=1170 Shopping Offers Time Limit: 1000MSMemory Limit: 10000K 问题描述 In ...
- DPDK flow_filtering 源码阅读
代码部分 main.c /*- * BSD LICENSE * * Copyright 2017 Mellanox. * * Redistribution and use in source and ...
- 初期测评 A 排序
https://vjudge.net/contest/240302#problem/A 输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0 ...
- lucene介绍
1.https://blog.csdn.net/shuaicihai/article/details/65111523 2.https://www.cnblogs.com/rodge-run/p/65 ...
- DBGrid添加行号编写笔记
procedure TForm1.ClientDataSet1NewRecord(DataSet: TDataSet); begin ShowMessage('你好'); ClientDataSet1 ...
- 【uoj#311】[UNR #2]积劳成疾 dp
题目描述 一个长度为 $n$ 的序列,每个数在 $[1,n]$ 之间.给出 $m$ ,求所有序列的 $\prod_{i=1}^{n-m+1}(\text{Max}_{j=i}^{j+m-1}a[j]) ...
- 虚拟机VMware的安装
什么是虚拟软件: 虚拟软件是一个可以使你在一台机器上同时运行二个或更多Windows.LINUX等系统.它可以模拟一个标准PC环境.这个环境和真实的计算机一样,都有芯片组.CPU.内存.显卡.声卡.网 ...
- 只会java,参加acm如何?
作者:董适链接:https://www.zhihu.com/question/31213070/answer/51054677来源:知乎著作权归作者所有,转载请联系作者获得授权. 当然合适,有什么不合 ...
- ORA-01034和ORA-27101的解决方法
问题所在: 1.要登录的数据库实例内容配置内容错误,联系负责该机子的管理员看原因
- MySql数据库迁移图文展示
MySql数据库的数据从一台服务器迁移到另外一台服务器需要将数据库导出,再从另外一台服务器导入.方法有很多,MySql配套的相关工具都有这个功能.phpMyAdmin就可以做,但是这个加载起来慢,推荐 ...