题意:给定一个数组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相关推荐

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

  2. Leetcode 565. Array Nesting

    题目 链接:https://leetcode.com/problems/array-nesting/ Level: Medium Discription: A zero-indexed array A ...

  3. [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 ...

  4. Leetcode——565. Array Nesting

    题目原址 https://leetcode.com/problems/array-nesting/description/ 题目描述 A zero-indexed array A of length ...

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

  6. [LeetCode] 565. Array Nesting

    565. Array Nesting 这道题目的大概意思是,先选定数组中一个起始的位置,再根据她的值定位到相应的下标,继续下去,直到出现循环为止,最后找出最长的不循环的. 显然需要将数组中每个位置起始 ...

  7. leetcode 954. Array of Doubled Pairs | 954. 二倍数对数组(Java)

    题目 https://leetcode.com/problems/array-of-doubled-pairs/ 题解 对于每一个数n来说,它要么和 n / 2 凑一对,要么和 n * 2 凑一对. ...

  8. LeetCode 561. Array Partition I

    题目: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...

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

最新文章

  1. python实现matlab中的diff,Python实现matlab数据绘制
  2. c# 获取所有的进程的cpu使用率_Linux CPU使用率很高,但为啥却找不到高CPU的进程
  3. SAP传输停止错误:Test call of transport control program (tp) ended with return code 0208!
  4. linux mysql进阶_mysql进阶学习二之搭建主从
  5. spring mvc+junit
  6. how to deal with error message Failed to create note
  7. 【HYSBZ - 1192】鬼谷子的钱袋(水题,二进制)
  8. c语言stanf,stanf
  9. Java 持有对象简要笔记
  10. Q108:浅析PBRT-V3的代码结构
  11. 使用EqualsBuilder和HashCodeBuilder生成hashcode和equal方法
  12. 优秀的软件测试简历是什么样的?
  13. MapReduce实现kmeans算法
  14. 常见互联网公司职级和薪资一览,有条件的一定要进大厂,薪水是真高
  15. Ubuntu系统垃圾清理
  16. thinkpad笔记本电脑怎么关闭触摸板
  17. uoj 36 玛里苟斯
  18. 基于单片机的地铁(公交)无线控制报站系统设计 地铁报站系统设计 公交报站系统设计
  19. 为什么说贴片式T卡完胜T卡?
  20. php gb18030 utf-8,GB18030和UTF-8有啥区别

热门文章

  1. 如何用手机观看群晖ds218play上视频
  2. 目前计算机主流配置及选购,计算机主流配置及选购.doc
  3. [基础]-beautifulsoup模块使用详解
  4. [进阶]-多线程、多进程、异步IO实用例子
  5. ubuntu20.4安装anaconda和pycharm
  6. 用互联网思想武装自己---雷军
  7. 深入理解空间金字塔池化(SPP,ASPP)
  8. 人工智能有哪些优势?人工智能5项技术介绍
  9. scrcpy投屏教程、及无线投屏
  10. 网络计算机记住用户名,电脑网站怎样设置记住账号密码怎么办