8ms C++ solution


  • 1
    class Solution {
    public:
        int minPatches(vector<int>& nums, int n) {
            unsigned int farNum = 0;
            int idx = 0, padding = 0;
        
            while(farNum < n) {
                if( idx < nums.size() && nums[idx] <= farNum+1 ) {
                    farNum += nums[idx++];
                } else {
                    padding++;
                    farNum =  farNum*2+1;
                }
            }
            return padding;
        }
    };
    `

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.