编辑代码

/**
 * @param {number[]} nums
 * @return {number}
 */
var deleteAndEarn = function(nums) {
    //console.log(nums)
    const n = Math.max(...nums) + 1;
    var points = new Array(n).fill(0);
    var cache = new Array(n).fill(0);
    for (var i in nums){
        //console.log(nums[i])
        num = nums[i]
        if (points[num] >= num){
            points[num] +=  num;
            //console.log('found more num', num, 'added')
        }
        else{points[num] = num;}
    }
        
    //console.log(points)
    cache[1] = points[1]
    for (var j = 2; j<n; j++){
        cache[j] = Math.max(cache[j-1], (cache[j-2] + points[j]))
        console.log(cache)
    }
    
    
    //return(cache[j])
    

};




// test input

var n = 5;
var elements = [5,6,6,4,11];
deleteAndEarn(elements)