198. House Robber 213. House Robber II 隔家盗窃

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

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.

Example 2:

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.

题目:一条街道有许多的店shops[n],每家店有数量不等的存折money[x],作为小偷,不能同时盗取两家相邻的店,求能盗取的最大数额。

思路:将店面以0至n进行编号,然后以某个店shops[x]为例,如果当前店shops[x]的存折加上隔壁的隔壁shops[x-2]累积的存折大于隔壁shops[x-1]累积的存折,那就偷两家,否则只偷隔壁一家。

totalMoney[x] = max (money[x] + totalMoney[x-2] , totalMoney[x-1])

关于totalMoeny逐步累计的方法

shops[0]、shops[1]的累计总额为店本身的存折总额

totalMoney[0] = money[0]

totalMoney[1] = money[1]

shop[2]得到累计总额则取决于shops[0]+shops[2]与shops[1]的最大值

totalMoney[2] = max ( money[2] + totalMoney[0] , totalMoney[1] )

shop[3]得到累计总额则取决于自身x与x-2及之前的累计总额,与隔壁累计总额相比较的最大值

totalMoney[3] = {\color{Blue} max }( money[3] +{\color{Blue} max}{\color{Red} ( }totalMoney[0] ,totalMoney[1]{\color{Red} )}, totalMoney[2] )

总结以上:totalMoney[]数组存的是当前店铺能盗取的最大存款总额,当轮到一个店时,店的前两个店(0、1除外)已经存好了它最大的总额,所以只需比较 totalMoney[n] + totalMoney[n-2] 与  totalMoney[n-1] 即可知道最大总额。

代码:

class Solution {
public:   

    vector<int> dp;  //店铺累计最大额度
    int f( int i,vector<int>& nums){
        if(i<2) return nums[i];
        if(dp[i]!=-1) return dp[i];
        for(int j=i-2;j>=0;j--){  //求前n-2及之前店铺的最大值
            dp[i]=max(dp[i],nums[i]+f(j,nums));
        }
        return dp[i];
    }
    
    int rob(vector<int>& nums) {
        dp.resize(nums.size(),-1);
        int ans =0;
        for(int i =0 ; i<nums.size();i++){
            ans = max(ans,f(i,nums));
        }    
        return ans ;
    }
};

题目:House Robber II 与 House Robber 区别在于II中的店铺首尾相连,不能同时盗窃第一个和最后一个。

思路:与1中的方法类似,因为店铺首尾相连,所以第1家和最后一家只能选择一家盗取,设总店铺数为n,分别找出第1家到第n-1家范围内能盗取的最大财务,与第2家到第n家范围内能盗取的财务,然取两个的最大值,即为能盗取的最大财务。

代码:

class Solution {
public:   

    int maxmoney(vector<int>& nums,int first,int last){
        int pre =0,cur =0;    //pre代表相隔的店铺,cur代表相邻的店铺
        for(int i=first;i<=last;i++){
           //取当前店铺加上相隔店铺的总和 与相邻店铺的总和进行的比较
            int curmax = max(pre+nums[i],cur);  
            pre = cur ;
            cur = curmax;
        }
        return cur;  //最后店铺累计的最大值
    }
 
    int rob(vector<int>& nums) {
        int n = nums.size();
        if(n<2) 
            return n?nums[0]:0;
        return max(maxmoney(nums,0,n-2),maxmoney(nums,1,n-1));
    }
};

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值