My 3ms Javascript DP solution using an object literal


  • 1
    C

    Thanks for guys

    var combinationSum4 = function(nums, target) {
        var cache = {};
        for(var i=1; i<=target; i++){
            for(var j=0; j<nums.length; j++){
                if(!cache[i]) cache[i]=0;
    
                if(i == nums[j]) cache[i]++;
                else if(i > nums[j]) cache[i] += cache[i-nums[j]];
            }
        }
        return cache[target] ? cache[target] : 0;
    };
    

Log in to reply
 

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