算法设计与结构基础作业第十周

198.House Robber

Description:

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.

Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.


分析:

       dp[i]表示到第i个住户所能盗取的最大数额,a[i] 为 在第i个住户处能盗取的数额,dp[n]即为问题所求。考虑最后一个住户,只有两种情况:如果选择盗取该住户,那么需要跳过倒数第二个住户,这种情况下所能得到的最大数额为a[i] + dp[i-2],如果不盗取该住户,那么最大数额与dp[i-1]一致。取两种情况中的大者,即为dp[i]。这样问题被分解为规模-1-2的重叠子问题。然后正向递推得到最终的解。


My C++ code:

class Solution {
public:
    int rob(vector<int>& nums) {
        vector<int> dp(nums.size()+1, 0);
        for(vector<int>::size_type i = 0; i < nums.size(); ++i)
        {
            dp[i+1] = max(dp[i], dp[i-1]+nums[i]);
        }
        return dp[nums.size()];
    }
};


213.House Robber II

Description:

Note: This is an extension of House Robber.

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.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases


分析:

    和上题差不多,考虑最后一个住户,分为两种情况:盗取最后一个住户,此时第一个住户也可以盗取,那么这种情况下的最大数额由第一个住户到倒数第二个住户按前述算法得到;不盗取最后一个住户,这种情况下的最大数额由第二个住户到最后一个住户由前述算法得到。最终答案为大者。

My C++code:

class Solution {
public:
    int rob(vector<int>& nums) {
        if(nums.size() == 0)
        {
            return 0;
        }
        if(nums.size() == 1)
        {
            return nums[0];
        }
        vector<int> dp1(nums.size()+1, 0);
        vector<int> dp2(nums.size()+1, 0);
        for(vector<int>::size_type i = 0; i < nums.size() - 1; ++i)
        {
            dp1[i+1] = max(dp1[i], dp1[i-1]+nums[i]);
        }
        for(vector<int>::size_type i = 1; i < nums.size(); ++i)
        {
            dp2[i+1] = max(dp2[i], dp2[i-1]+nums[i]);
        }
        return max(dp1[nums.size()-1], dp2[nums.size()]);
    }
};

  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值