pwstrick / daily

一份搜集的前端面试题目清单、面试相关以及各类学习的资料(不局限于前端)
2.39k stars 242 forks source link

打家劫舍 #1051

Open pwstrick opened 4 years ago

pwstrick commented 4 years ago

198. 打家劫舍

/**
 * @param {number[]} nums
 * @return {number}
 */
var rob = function(nums) {
    let dp = [];
    if(nums.length == 0)
        return 0;
    if(nums.length == 1)
        return nums[0];
    if(nums.length == 2)
        return Math.max(nums[0], nums[1]);
    dp.push(nums[0]);
    dp.push(Math.max(nums[0], nums[1]));
    for(let i=2, len=nums.length; i<len; i++) {
        dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i]);
    }
    return dp[dp.length - 1];
};
zhangdavids commented 3 years ago

感觉你们大前端真心好啊👍