zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Codeforces 916E Jamie and Tree (换根讨论)
    BZOJ 3083 遥远的国度 (换根讨论 + 树链剖分)
    Codeforces 703D Mishka and Interesting sum(离线 + 树状数组)
    ECNU 3480 没用的函数 (ST表预处理 + GCD性质)
    HDU 4343 Interval query(贪心 + 倍增)
    Codeforces 147B Smile House(DP预处理 + 倍增)
    HDU 4870 Rating (高斯消元)
    TopCoder SRM 301 Div2 Problem 1000 CorrectingParenthesization(区间DP)
    Hrbust 2320 OX (博弈)
    Hrbust 2319 Number Game(贪心)
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4980366.html
Copyright © 2011-2022 走看看