leetcode Patching Array
题意:给定一个数组nums和一个数n,求添加最少的数使得[1,n]中的每个数都可以由数组中元素和组成用known_sum表示已知的连续和为[1,known_sum),有了这个表示那就简单了:
- nums[i] <= known_sum,更新已知范围为:[1,known_sum + nums[i] )
- nums[i] > known_sum, 添加known_sum进数组才能达到最大的范围,所以已知范围更新为:[1,known_sum *2 )
class Solution {
public:int minPatches(vector<int>& nums, int n) {int cnt = 0, i = 0;for (long known_sum = 1; known_sum <= n;) {if (i < nums.size() && nums[i] <= known_sum) {known_sum += nums[i++];}else {known_sum <<= 1;cnt++;}}return cnt;}
};
本文是leetcode 330 Patching Array 的题解,更多题解可见
https://www.hrwhisper.me/leetcode-algorithm-solution/
本文由 hrwhisper 原创发布
出处:细语呢喃 > leetcode Patching Array
地址:https://www.hrwhisper.me/leetcode-patching-array/
leetcode Patching Array相关推荐
- leetcode 330. Patching Array | 1798. Maximum Number of Consecutive Values You Can Make
1798. Maximum Number of Consecutive Values You Can Make | 1798. 你能构造出连续值的最大数目 https://leetcode.com/p ...
- Leetcode 565. Array Nesting
题目 链接:https://leetcode.com/problems/array-nesting/ Level: Medium Discription: A zero-indexed array A ...
- [LeetCode]561. Array Partition I (数组分区 1)
561. Array Partition I Given an array of 2n integers, your task is to group these integers into n pa ...
- Leetcode——565. Array Nesting
题目原址 https://leetcode.com/problems/array-nesting/description/ 题目描述 A zero-indexed array A of length ...
- 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] 565. Array Nesting
565. Array Nesting 这道题目的大概意思是,先选定数组中一个起始的位置,再根据她的值定位到相应的下标,继续下去,直到出现循环为止,最后找出最长的不循环的. 显然需要将数组中每个位置起始 ...
- leetcode 954. Array of Doubled Pairs | 954. 二倍数对数组(Java)
题目 https://leetcode.com/problems/array-of-doubled-pairs/ 题解 对于每一个数n来说,它要么和 n / 2 凑一对,要么和 n * 2 凑一对. ...
- LeetCode 561. Array Partition I
题目: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...
- 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 ...
最新文章
- python实现matlab中的diff,Python实现matlab数据绘制
- c# 获取所有的进程的cpu使用率_Linux CPU使用率很高,但为啥却找不到高CPU的进程
- SAP传输停止错误:Test call of transport control program (tp) ended with return code 0208!
- linux mysql进阶_mysql进阶学习二之搭建主从
- spring mvc+junit
- how to deal with error message Failed to create note
- 【HYSBZ - 1192】鬼谷子的钱袋(水题,二进制)
- c语言stanf,stanf
- Java 持有对象简要笔记
- Q108:浅析PBRT-V3的代码结构
- 使用EqualsBuilder和HashCodeBuilder生成hashcode和equal方法
- 优秀的软件测试简历是什么样的?
- MapReduce实现kmeans算法
- 常见互联网公司职级和薪资一览,有条件的一定要进大厂,薪水是真高
- Ubuntu系统垃圾清理
- thinkpad笔记本电脑怎么关闭触摸板
- uoj 36 玛里苟斯
- 基于单片机的地铁(公交)无线控制报站系统设计 地铁报站系统设计 公交报站系统设计
- 为什么说贴片式T卡完胜T卡?
- php gb18030 utf-8,GB18030和UTF-8有啥区别