213. House Robber II

本文深入解析了LeetCode上关于House Robber II的问题,详细讲解了问题的难点、解决思路以及实现方法。通过分析House Robber的基本概念,引入了圆环型房屋布局的挑战,并给出了优化解决方案,包括构建辅助函数、处理边界条件和计算最大收益的步骤。此外,文章还提供了不同层次的代码实现,从初次尝试到熟练掌握,逐步深入理解动态规划在解决此类问题中的应用。

题目:

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.

链接: http://leetcode.com/problems/house-robber-ii/

题解:

乍一看感觉比较棘手,于是去看了discuss。这个问题比较tricky,但想清楚以后就很简单。因为House 1和House n相连,所以我们要么rob House 1,要么rob House n,两者不可兼得。于是我们只要比较rob(nums, 0, n - 2)与rob(nuns,1, n - 1)这两个值就可以了,其他部分和House Rob基本一样,都是使用DP。 (和House Rob一起要好好思考如何构建辅助函数)

Time Complexity - O(n), Space Complexity - O(n)

public class Solution {
    public int rob(int[] nums) {
        if(nums == null || nums.length == 0)
            return 0;
        if(nums.length == 1)
            return nums[0];
        return Math.max(rob(nums, 0, nums.length - 1), rob(nums, 1, nums.length));  
    }
    
    private int rob(int[] nums, int lo, int hi) {
        int pre = 0, prePre = 0, max = 0;
        
        for(int i = lo; i < hi; i++) {
            if(i - 2 < lo)
                prePre = 0;
            if(i - 1 < lo)
                pre = 0;
            max = Math.max(nums[i] + prePre, pre);
            prePre = pre;
            pre = max;
        }
        
        return max;
    }
}

 

二刷:

二刷就比较顺了,就是第一个房子的取舍问题。我们可以建立一个辅助方法来决定我们dp的范围。

这里要注意的是nums.length == 1的时候我们可以直接返回nums[0]。

Java:

public class Solution {
    public int rob(int[] nums) {
        if (nums == null) return 0;
        if (nums.length == 1) return nums[0];
        return Math.max(rob(nums, 0, nums.length - 2), rob(nums, 1, nums.length - 1));
    }
    
    private int rob(int[] nums, int lo, int hi) {int res = 0, robLastHouse = 0, notRobLast = 0;
        for (int i = lo; i <= hi; i++) {
            res = Math.max(robLastHouse, notRobLast + nums[i]);
            notRobLast = robLastHouse;
            robLastHouse = res;
        }
        return res;
    }
}

 

三刷:

Java:

public class Solution {
    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        if (nums.length == 1) return nums[0];
        return Math.max(rob(nums, 0, nums.length - 2), rob(nums, 1, nums.length - 1));
    }
    
    private int rob(int[] nums, int lo, int hi) {
        if (nums == null || lo > hi) return 0;
        int robLast = 0, notRobLast = 0, res = 0;
        for (int i = lo; i <= hi; i++) {
            res = Math.max(robLast, notRobLast + nums[i]);
            notRobLast = robLast;
            robLast = res;
        }
        return res;
    }
}

 

 

Reference:

https://leetcode.com/discuss/36544/simple-ac-solution-in-java-in-o-n-with-explanation

https://leetcode.com/discuss/36770/9-lines-0ms-o-1-space-c-solution

https://leetcode.com/discuss/57601/good-performance-dp-solution-using-java

转载于:https://www.cnblogs.com/yrbbest/p/4980366.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值