【leetcode】House Robber & House Robber II(middle)

本文探讨了一种称为“抢劫者问题”的经典动态规划问题。该问题要求在一个街区或环形排列的房屋中,每栋房屋都有一定的金钱数量,在不能连续抢劫相邻房屋的约束下,找出能获得最大金额的抢劫方案。文章提供了多种解决方案,包括递归、动态规划等,并对比了不同方法的空间复杂度。

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.

 

思路:开始写了个递归超时了。反应了好久才反应过来要用动态规划。

递归代码:

int rob(vector<int> &num) {
        return robsub(num, 0, num.size());
    }
    int robsub(vector<int> &num, int s, int e)
    {
        if(s == e) return 0;
        if(e - s == 1) return num[s];
        return max(robsub(num, s + 1, e), num[s] + ((e - s >= 3) ? robsub(num, s + 2, e) : 0));
    }

动态规划:

int rob2(vector<int> &num) {
        vector<int> dp(num.size() + 1, 0); //截止到num[i] - 1时的最大值
        int ans = 0;
        for(int i = 1; i <= num.size(); i++)
        {
            dp[i] = num[i - 1] + max(((i - 2) >= 0 ? dp[i - 2] : 0), ((i - 3) >= 0 ? dp[i - 3] : 0));
            ans = (ans > dp[i]) ? ans : dp[i];
        }
        return ans;
    }

 

我的动态规划代码用的空间太多了,其实只要两个变量记录一下前面的就好。

public class Solution {
    public int rob(int[] num) {
        int i = 0;
        int e = 0;
        for (int k = 0; k<num.length; k++) {
            int tmp = i;
            i = num[k] + e;
            e = Math.max(tmp, e);
        }
        return Math.max(i,e);
    }
}

 

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

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.

 

思路:

时隔一个月做2,结果还是写递归。又是反应好久才想起来是动态规划。失败啊....

连成一个圈了,我们可以把问题分解为从房屋0到n-2和房屋1到n-1两个部分。

最优代码,dp,节约空间

int rob2(vector<int>& nums) {
        if(nums.empty()) return 0;
        if(nums.size() == 1) return nums[0];
        int maxMoney = 0;
        int pre = 0, cur = 0;
        //抢第一间房屋 不抢最后一间房屋的情况
        for(int i = 0; i < nums.size() - 1; ++i)
        {
            int tmp = cur;
            cur = nums[i] + pre;
            pre = max(tmp, pre); //取前一个值和前前一个值中较大的
        }
        maxMoney = max(cur, pre);
        //不抢第一间房屋 抢最后一间的情况
        cur = pre = 0;
        for(int i = 1; i < nums.size(); ++i)
        {
            int tmp = cur;
            cur = nums[i] + pre;
            pre = max(tmp, pre);
        }
        maxMoney = max(maxMoney, max(cur, pre));
        return maxMoney;
    }

 

次优代码,dp, 数组存储

int rob(vector<int>& nums) {
        if(nums.empty()) return 0;
        if(nums.size() == 1) return nums[0];
        int maxMoney = 0;
        vector<int> dp1(nums.size(), 0);
        vector<int> dp2(nums.size(), 0);
        //抢第一间房屋的情况
        for(int i = 0; i < nums.size() - 1; ++i)
        {
            dp1[i] = nums[i] + max((i - 2 >= 0) ? dp1[i - 2] : 0, (i - 3 >= 0) ? dp1[i - 3] : 0);
            maxMoney = (dp1[i] > maxMoney) ? dp1[i] : maxMoney;
        }
        //不抢第一间房屋的情况
        for(int i = 1; i < nums.size(); ++i)
        {
            dp2[i] = nums[i] + max((i - 2 >= 1) ? dp2[i - 2] : 0, (i - 3 >= 1) ? dp2[i - 3] : 0);
            maxMoney = (dp2[i] > maxMoney) ? dp2[i] : maxMoney;
        }
        return maxMoney;
    }

 

最烂的递归代码,超时。

//递归 超时
    int rob1(vector<int>& nums) {
        if(nums.empty()) return 0;
        int maxMoney = 0;
        int curMoney = 0;
        recursion(maxMoney, curMoney, 1, nums, false);
        curMoney = nums[0];
        recursion(maxMoney, curMoney, 2, nums, true);
        return maxMoney;
    }

    void recursion(int &maxMoney, int &curMoney, int id, vector<int>& nums, bool isFirstUsed)
    {
        if(id >= nums.size())
        {
            maxMoney = (curMoney > maxMoney) ? curMoney : maxMoney;
        }
        else if(id == nums.size() - 1 && isFirstUsed) //当前是最后一个屋子 但第一个屋子抢过
        {
            recursion(maxMoney, curMoney, id + 1, nums, isFirstUsed); //不抢当前房屋
        }
        else
        {
            int money = nums[id];
            recursion(maxMoney, curMoney, id + 1, nums, isFirstUsed); //不抢当前房屋
            curMoney += money; //抢当前房屋
            recursion(maxMoney, curMoney, id + 2, nums, isFirstUsed);
            curMoney -= money;
        }
    }

 

转载于:https://www.cnblogs.com/dplearning/p/4433519.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值