JavaScript Solution


  • 0
    T
    /**
     * @param {number[]} nums
     * @return {number}
     */
    const deleteAndEarn = function(nums) {
        const val = Array(10001).fill(0);
        
        nums.forEach((n, i) => {
            val[n] += n;
        });
        
        let pick = 0;
        let nonPick = 0;
        val.forEach((n, i) => {
            const tmp = pick;
            pick = nonPick + val[i];
            nonPick = _.max([tmp, nonPick]);
        });
        
        return _.max([pick, nonPick]);
    };
    

Log in to reply
 

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