leetcode 330. 按要求补齐数组 【贪心构造】
思路参考:https://leetcode-cn.com/problems/patching-array/solution/an-yao-qiu-bu-qi-shu-zu-tan-xin-suan-fa-b4bwr/
当前集合可以覆盖[1,b],在当前集合上增加一个数字,使得这个区间可以覆盖到的数组最多,并且保证区间没有产生空隙
#define debug(x) cout<<#x<<": "<<(x)<<endl;
using ll = long long;
class Solution {
public:
int minPatches(vector<int>& a, int n) {
ll r = 0;
int ret = 0;
int i = 0;
while(r < n){
if(i<a.size() && r+1 >= a[i]){
r += a[i];
i++;
}
else { //r+1 < a[i])
ret ++;
r += (r+1);
}
}
return ret;
}
};
还没有评论,来说两句吧...