码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 198 House Robber I

时间:2020-01-14 13:23:03      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:UNC   max   array   use   script   else   turn   leetcode   etc   

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

标签:UNC   max   array   use   script   else   turn   leetcode   etc   

原文地址:https://www.cnblogs.com/rubylouvre/p/12191387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!