213. House Robber II

题目:

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

posted @ 2015-11-20 12:31  YRB  阅读(639)  评论(0编辑  收藏  举报