[LeetCode]Patching Array
题目:Patching Array
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n]
inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required.
Example 1:
nums = [1, 3]
, n = 6
Return 1
.
Combinations of nums are [1], [3], [1,3]
, which form possible sums of: 1, 3, 4
.
Now if we add/patch 2
to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3]
.
Possible sums are 1, 2, 3, 4, 5, 6
, which now covers the range [1, 6]
.
So we only need 1
patch.
Example 2:
nums = [1, 5, 10]
, n = 20
Return 2
.
The two patches can be [2, 4]
.
Example 3:
nums = [1, 2, 2]
, n = 5
Return 0
.
题目:
给定一个递增数组和正整数n,找到最少的缺失的数字,使得使用递增数组中的数字和它们的和能包含[1,n]中的所有数字。
思路:
看似复杂实际上很简单,只要能够发现,任何数字int型数字可以使用32位二进制表示,如果在递增的数组中包含所有的二进制的位数对应的数(1,2,4,8,16,...,1024,...)就必然能表示出[1,n]中的所有数字。
进一步我们可以发现当有这样的情况发生时,则必然需要补充数字:
当前k个数的和小于第k+1个数时,其前k个数的和 + 1的数字必须要补充,类似二进制中进位的思想。
这样实际上只需要不停地求前n项和,在和下一个数字比较就能知道那些数字需要补充。
/** 在[1,n]之间的任何一个数i的二进制形式是由01组成,所以可以使用1,2,4,...的和来组成i; 因此,只需要判断数组中的元素有没有1,2,4,...或能组成上面这些数的数组合。 **/ int LeetCode::minPatches(vector<int>& nums, int n){long long m = 1;int i = 0, patch = 0;//统计patch的数量while (m <= n){if (i < nums.size() && nums[i] <= m){m += nums[i++];//如果nums[i] == m时,m翻倍;反之m会增加nums[i]的大小;经验可知,只要有1,2以及(n,2n)之间的数k可以得到2n。 }else{//m在nums中不存在,或者数组的数值不够m <<= 1;++patch;}}return patch; }
其中,注意
m <<= 1;
补充了元素之后前n项和还要加上补充的元素的值。这里之间变成两倍。
转载于:https://www.cnblogs.com/yeqluofwupheng/p/7041478.html
[LeetCode]Patching Array相关推荐
- leetcode Patching Array
题意:给定一个数组nums和一个数n,求添加最少的数使得[1,n]中的每个数都可以由数组中元素和组成用known_sum表示已知的连续和为[1,known_sum),有了这个表示那就简单了: nums ...
- 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 ...
最新文章
- Solr相似度算法一:Lucene TF-IDF 相关性算分公式
- Linux学习:静态库和动态库
- BW之数据源 增量管理DELTA
- layui绑定json_JSON绑定:概述系列
- 贪心算法---背包问题(物品可以分割问题)
- 为什么使用NativeJdbcExtractor
- JQuery和php方面的填空题,PHP复习题(问答题)
- 【李宏毅2020 ML/DL】补充:Structured Learning: Structured SVM
- dict字典,以及字典的一些基本应用
- 9种缺陷管理工具对比Bugzilla、testRail、TestLink、redmine、禅道、CODING、jira、TAPD、PingCode
- 苹果手机显示有4g上不了网络连接服务器,苹果移动数据为什么叫蜂窝网络_蜂窝移动数据和4g有什么区别...
- web h5调用微信分享功能
- Incapsula reese84 分析与破解
- LTE-5G学习笔记18--5G关键技术(想学更多快进群)
- Leetcode学习之贪心算法
- 从源码的角度理解Glide的执行流程
- VBA—EXCEL操作集合—07
- DFT,IDFT,FFT,IFFT算法的C++实现
- 互联网摸鱼日报(2022-09-25)
- 先梳理好思维,再动手实战这样会事半功倍
热门文章
- java文件上传中间件_maven工程 java 实现文件上传 SSM ajax异步请求上传
- php爬取网站所有链接,php 爬取超链接
- java构造方法编写_Java开发 Debug运行+类的编写+构造方法+方法重载+this【学习篇6】...
- linq判断集合中相同元素个数_高中数学:集合与函数概念知识点汇总
- 同步方案java_【Java基础】多线程中同步的两种解决方案
- 查看已安装java插件_记录下eclipse下的插件安装,查看及删除方式
- .net vue漂亮登录界面_一文弄懂前端框架Vue 的核心——数据绑定,为升职涨薪加分
- 华为模拟器ensp与真实机器连接使用的配置内容一
- redis smembersmap_Redis五种数据类型
- 手挽手带你学VUE:四档 Vue-cli3 Vuex Vue-router