LintCode 392: House Robber (DP经典题!)

探讨了一个专业抢劫者如何在不触动相邻房屋警报系统的情况下,从一排房屋中获得最大金额的问题。通过动态规划算法,提供了多种解决方案,包括经典前缀型DP、滚动数组优化、坐标型DP以及空间优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 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.

Example
Example 1:

Input: [3, 8, 4]
Output: 8
Explanation: Just rob the second house.
Example 2:

Input: [5, 2, 1, 3]
Output: 8
Explanation: Rob the first and the last house.
Challenge
O(n) time and O(1) memory.

解法1:经典前缀型DP。dp[i]表示rob到house i的时候累计所得到的最大值。
dp[i] = dp[i - 2] + A[i] > dp[i - 1] ? dp[i - 2] + A[i] : dp[i - 1]

代码如下:

class Solution {
public:
    /**
     * @param A: An array of non-negative integers
     * @return: The maximum amount of money you can rob tonight
     */
    long long houseRobber(vector<int> &A) {
        int len = A.size();
        if (len == 0) return 0;
        if (len == 1) return A[0];
        
        vector<long long> dp(len, 0);
        dp[0] = A[0];
        dp[1] = A[0] > A[1] ? A[0] : A[1];
        
        for (int i = 2; i < len; ++i) {
            dp[i] = dp[i - 2] + A[i] > dp[i - 1] ? dp[i - 2] + A[i] : dp[i - 1];
        }
        
        return dp[len - 1];
    }
};

2刷
dp[i] = max(dp[i - 2] + A[i - 1], dp[i - 1]);
顺便说一下,为什么max里面是dp[i-2]+A[i-1]而肯定不是dp[i-3]+A[i-2]呢?因为这里面dp[]肯定是一个单调非递增的数组,dp[i-2]肯定比dp[i-3]大。

class Solution {
public:
    /**
     * @param A: An array of non-negative integers
     * @return: The maximum amount of money you can rob tonight
     */
    long long houseRobber(vector<int> &A) {
        int len = A.size();
        if (len == 0) return 0;
        if (len == 1) return A[0];
        
        vector<long long> dp(len + 1, 0);
        dp[1] = A[0];
        for (int i = 2; i <= len; ++i) {
            dp[i] = max(dp[i - 2] + A[i - 1], dp[i - 1]);
        }
        
        return dp[len];
    }
};

解法2:解法1加上滚动数组优化。空间O(1)。
代码如下:

class Solution {
public:
    /**
     * @param A: An array of non-negative integers
     * @return: The maximum amount of money you can rob tonight
     */
    long long houseRobber(vector<int> &A) {
        int len = A.size();
        if (len == 0) return 0;
        if (len == 1) return A[0];
        
        long long dp1 = A[0];
        long long dp2 = A[0] > A[1] ? A[0] : A[1];

        for (int i = 2; i < len; ++i) {
            long long dp = dp1 + A[i] > dp2 ? dp1 + A[i] : dp2;
            dp1 = dp2;
            dp2 = dp;
        }
        
        return dp2;
    }
};

解法3:坐标型DP,多加一个状态表示rob the house or not
dp[i][0] - maximum value at house i if does not rob the house i
dp[i][1] - maximum value at house i if rob the house i
注意: 如果rib house i,dp[i][1]应为dp[i-2][1]和dp[i-2][0]的最大值。这样的话,dp[i][1]的值可能比下面2种选项小些,但是没关系,最优解要么在dp[i][1]中,要么在dp[i][0]中,而dp[i][0]的值是能保证最优的。
这里其实也可以取dp[i-2][1]和dp[i-1][0]的最大值,因为dp[i-1][0]肯定>=dp[i-2][1]。也可以直接取dp[i-1][0],更简单。

class Solution {
public:
    /**
     * @param A: An array of non-negative integers
     * @return: The maximum amount of money you can rob tonight
     */
    long long houseRobber(vector<int> &A) {
        int len = A.size();
        if (len == 0) return 0;
        if (len == 1) return A[0];
        
        vector<vector<long long>> dp(len + 1, vector<long long>(2));
   
        dp[1][1] = A[0];
        for (int i = 2; i <= len; ++i) {
            dp[i][0] = max(dp[i - 1][1], dp[i - 1][0]); //house i 不抢
            dp[i][1] = max(dp[i - 2][1], dp[i - 2][0]) + A[i - 1]; //house i 抢
            //注意,改成下面也对!
            //dp[i][1] = max(dp[i - 2][1], dp[i - 1][0]) + A[i - 1]; //house i 抢
            //改成下面也对,因为dp[i-1][0] >= dp[i-2][1]
            //dp[i][1] = dp[i - 1][0] + A[i - 1];
        }
        return max(dp[len][0], dp[len][1]);
    }
};

解法4:解法3+空间优化,可以优化到O(1)空间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值