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.
class Solution
{
public:
int minPatches(vector<int> &nums, int n)
{
long up = 1;
int ret = 0, i = 0;
while (up <= n)
{
if (i < nums.size() && nums[i] <= up)
{
up += nums[i++];
}
else
{
up <<= 1;
++ret;
}
}
return ret;
}
};