A simple JS solution.


  • 1
    A
    /**
     * @param {number[]} nums
     * @param {number} n
     * @return {number}
     */
    var minPatches = function(nums, n) {
        var sum = 0, patch = 0, i = 0, num = nums[0];
        
        while(sum < n){
            if(!num || sum + 1 < num){
                patch++;
                sum += sum + 1;
            }else if(num){
                sum += num;
                num = nums[++i];
            }
        }
        
        return patch;
    };

Log in to reply
 

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