Leetcode 330 Solution

This article provides solution to leetcode question 330 (patching-array)

https://leetcode.com/problems/patching-array

Solution

class Solution {
public:
    int minPatches(vector<int>& nums, int n) {
        int64_t miss = 1;
        int i = 0;
        int res = 0;

        while (miss <= n)
        {
            if (i < nums.size() && nums[i] <= miss)
                miss += nums[i++];
            else
                res++, miss += miss;
        }

        return res;
    }
};