【Leetcode】198. House Robber

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.

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.

这道题的本质相当于在一列数组中取出一个或多个不相邻数,使其和最大。

递归思路

从第 0 家开始偷,假设第 0 家偷,因为不能偷相邻的,那么下次就是从 [2,n] 开始,如果第 0 家不偷,那么下次就是从 [1,n] 开始偷。一直递归直到最后一家。

解法1:暴力枚举

class Solution {
    public int rob(int[] nums) {
        return rob(0,nums);
    }
    //1、思考递归退出的边界条件
    //2、写递归过程
    //3、返回结果
	public int rob(int idx,int[] nums) {
        if(idx >= nums.length){
            return 0;
        }
       int a = nums[idx] + rob(idx+2,nums);//假设第一家偷,那下一家就是 idx+2,进行递归
        int b = 0 + rob(idx+1,nums);//第一家不偷,则为0,下一家就是 idx+1,再进行递归
        int c = Math.max(a,b);
        return c;
	}
}

解法2:加缓存

暴力枚举在 Leetcode 会超时,而在上面的递归树中,有很多重复递归遍历的节点,这里就需要增加缓存,如果已经遍历过,就直接返回。

class Solution {
    private Map<Integer,Integer> cache = new HashMap<>();
    public int rob(int[] nums) {
        cache.clear();//缓存之前先清理缓存
        return rob(idx,nums);
    }
    //1、思考递归退出的边界条件
    //2、缓存判断在边界条件后执行
    //3、写递归过程
    //4、在返回结果的地方进行缓存
    //5、返回结果
    public int rob(int idx,int[] nums) {
        if(idx >= nums.length){
            return 0;
        }
        if(cache.containsKey(idx)) {
            return cache.get(idx);
        }
        int a = nums[idx] + rob(idx+2,nums);
        int b = 0 + rob(idx+1,nums);
        int c = Math.max(a,b);
        cache.put(idx,c);
        return c;
    }
}

解法3:循环变量

class Solution {
    public int rob(int[] nums) {
        return robot(nums);
    }
    //1、思考递归退出的边界条件
    //2、缓存判断在边界条件后执行
    //3、写递归过程
    //4、在返回结果的地方进行缓存
    //5、返回结果
    
    //Map缓存
    private Map<Integer,Integer> cache = new HashMap<>();
    public int robot(int[] nums) {
        if(nums==null || nums.length==0){
            return 0;
        }
        if(nums.length == 1) return nums[0];
        //清缓存
        cache.clear();
        int n = nums.length;
        //递归自下而上进行,从第 n 家开始偷,直到第一家为止
        //对比上面的递归算法,写出循环就比较简单
        cache.put(n-1,nums[n-1]);
        cache.put(n-2,Math.max(nums[n-1],nums[n-2]));
        for(int i=n-3; i>=0; --i){
            cache.put(i,Math.max(nums[i] + cache.get(i+2), cache.get(i+1)));
        }
        return cache.get(0);
    }
    
    //数组缓存
    private int[] caches = new int[10000];
    public int robot1(int[] nums) {
        if(nums==null || nums.length==0){
            return 0;
        }
        if(nums.length == 1) return nums[0];
        int n = nums.length;
        caches[n-1] = nums[n-1];
        caches[n-2] = Math.max(nums[n-1],nums[n-2]);
        for(int i=n-3; i>=0; --i){
            caches[i] = Math.max(nums[i] + caches[i+2], caches[i+1]);
        }
        return caches[0];
    }
}

DP 解题步骤

  1. 设计暴力算法,找到冗余
  2. 设计并存储状态(一维,二维,三维数组,甚至Map)
  3. 递归式(状态转移方程)
  4. 自底向下计算最优解(编程方式)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值