leetcode 198 House Robber I

function rob(nums) {
    if(!nums || nums.length === 0) {
        return 0;
    } else if(nums.length < 2){
        return nums[0];
    }
    let memo = new Array(nums.length);
    memo[0] = nums[0];
    memo[1] = Math.max(nums[0], nums[1]);
    for(let i = 2; i < nums.length; i++) {
        memo[i] = Math.max(memo[i-2]+nums[i], memo[i-1]);
    }
    return memo[memo.length-1];
}

你可能感兴趣的:(leetcode 198 House Robber I)