• 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

  • 相关阅读:
    About Face 摘录
    断言的使用
    C#中值传递和引用传递
    C++技巧之断言Assert
    About Face 一 目标导向设计
    About Face 二 设计行为与形态
    C++中引用传递与指针传递区别
    一个新的时代SoLoMo
    离散数学笔记算法部分
    汪教授的离散数学20110308 谓词与量词2
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4980366.html
Copyright © 2020-2023  润新知