[Dynamic Programming] 198. House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
             Total amount you can rob = 1 + 3 = 4.

Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
             Total amount you can rob = 2 + 9 + 1 = 12.

 

1 Form bottom-up approach

复制代码
/**
 * @param {number[]} nums
 * @return {number}
 */

var rob = function(nums) {
    // if there is nothing, return 0
    if (nums.length === 0) {
        return 0;
    }
    
    // if there is only one value, return itself
    if (nums.length === 1) {
        return nums[0];
    }
    
    // if there are two values, return the larger one
    if (nums.length === 2) {
        return Math.max(nums[0], nums[1]);
    }
    
    // if there are more than two values
    // copy the nums and preappend leading zero
    // to avoid extra if else check for sums[i - 3],
    // which can be out of index error
    let sums = [0].concat(nums);
    
    for (let i = 3; i < sums.length; i++) {
        sums[i] = Math.max(sums[i - 2] + sums[i], sums[i - 3] + sums[i]);
    }
    
    return Math.max(
        sums[sums.length - 1],
        sums[sums.length - 2]
    )
};
复制代码

 

2. Recursive:

复制代码
var rob = function(nums) {
    const helper = (nums, i, sums) => {
        // if there is nothing, return 0
        if (nums.length === 0) {
            return 0;
        }
        
        if (nums.length === 1) {
            return nums[0];
        }
        
        if (nums.length === 1) {
            return Math.max(nums[0], nums[1]);
        }

        // if there is only one value, return itself
        if (i === 0) {
            sums[0] = nums[0];
            return helper(nums, i+1, sums);
        }

        // if there are two values, return the larger one
        if (i === 1) {
            sums[1] = Math.max(nums[0], nums[1]);
            return helper(nums, i+1, sums);
        }
        
        if (i >= nums.length) {
            return Math.max(sums[sums.length - 1], sums[sums.length - 2]);
        }
        
        const step1 = sums[i-2] + nums[i];        
        const step2 = ( sums[i-3] || 0 ) + nums[i];
        
        const larger = Math.max(step1, step2);
        sums[i] = larger;
        
        return helper(nums, i+1, sums);
    };
    
    return helper(nums, 0, []);
}
复制代码

 

posted @   Zhentiw  阅读(147)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-12-02 [D3] Create Labels from Non-numeric Data with Ordinal Scales in D3 v4
2016-12-02 [D3] Create Labels from Numeric Data with Quantize Scales in D3 v4
2016-12-02 [D3] Convert Dates to Numeric Values with Time Scales in D3 v4
2016-12-02 [D3] Convert Input Data to Output Values with Linear Scales in D3
2016-12-02 [CSS] No selectable effect
2016-12-02 [Elm] Functions in Elm
2016-12-02 [Docker] Run Short-Lived Docker Containers
点击右上角即可分享
微信分享提示