lintcode:打劫房屋II

题目

打劫房屋II

在上次打劫完一条街道之后,窃贼又发现了一个新的可以打劫的地方,但这次所有的房子围成了一个圈,这就意味着第一间房子和最后一间房子是挨着的。每个房子都存放着特定金额的钱。你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动报警

给定一个非负整数列表,表示每个房子中存放的钱, 算一算,如果今晚去打劫,你最多可以得到多少钱 在不触动报警装置的情况下。

样例

给出nums = [3,6,4], 返回 6, 你不能打劫34所在的房间,因为它们围成一个圈,是相邻的.

解题

打劫房屋I  

根据题目,第0个房间和第n-1个房间只能打劫其中一个

打劫范围只能是:0-n-2 或者1-n-1

所以根据打劫房间I的程序,修改为根据范围进行打劫,再求这个两个打劫的最大值。

下面程序定义的dp数组,dp[i]表示打劫当前房间能够获得最大值

最后的结果要返回最后两个值得最大值

public class Solution {
    /**
     * @param nums: An array of non-negative integers.
     * return: The maximum amount of money you can rob tonight
     */
    public int houseRobber2(int[] nums) {
        // write your code here
        if(nums==null || nums.length == 0)
            return 0;
        if(nums.length == 1)
            return nums[0];
        if(nums.length == 2)
            return Math.max(nums[0],nums[1]);
        if(nums.length == 3)
            return Math.max(Math.max(nums[0],nums[1]),nums[2]);
        int len = nums.length;
        int res1 = houseRobber(nums,0,len-2);
        int res2 = houseRobber(nums,1,len-1);
        return Math.max(res1,res2);
    }
    public int houseRobber(int[] nums,int start,int end){
        if(start == end)
            return nums[start];
        if(start +1 == end){
            return Math.max(nums[start],nums[end]);
        }
        if(start +2 == end){
            return Math.max(nums[start]+nums[end],nums[start+1]);
        }
        int len = nums.length;
        int[] dp = new int[len];// 打劫 第i个房间,所能够获得最大值
        dp[start] = nums[start];
        dp[start+1] = nums[start+1];
        dp[start+2] = nums[start+2] + dp[start];
        for(int s = start + 3;s<= end;s++){
            dp[s] = nums[s] + Math.max(dp[s-3],dp[s-2]);
        }
        return Math.max(dp[end],dp[end-1]);
    }
}

 打劫房间I中下面定义的dp[i],表示打劫到第i个房间所能够获得0-i内的局部最大值

定义dp[i],表示当前所能获得的最大收获,这里的值最终就是最大值,数组dp的长度是len+1,第一个元素是0,dp[i]也可以理解为,不包含A[i]元素时所取得的最大值

dp[i] = Math.max(dp[i-1],dp[i-2]+A[i-1])

不是很理解

public class Solution {
    /**
     * @param nums: An array of non-negative integers.
     * return: The maximum amount of money you can rob tonight
     */
    public int houseRobber2(int[] nums) {
        // write your code here
        if(nums==null || nums.length == 0)
            return 0;
        if(nums.length == 1)
            return nums[0];
        if(nums.length == 2)
            return Math.max(nums[0],nums[1]);
        if(nums.length == 3)
            return Math.max(Math.max(nums[0],nums[1]),nums[2]);
        int len = nums.length;
        int res1 = houseRobber(nums,0,len-2);
        int res2 = houseRobber(nums,1,len-1);
        return Math.max(res1,res2);
    }
    public int houseRobber(int[] nums,int start,int end){
        
        int len = nums.length;
        int[] dp = new int[len+1];// 打劫 0-i之间的房间,所能够获得最大值
        dp[start] = 0;
        dp[start+1] = nums[start];
        
        for(int s = start+2;s<=end+1;s++){
            dp[s] = Math.max(dp[s-1],dp[s-2]+nums[s-1]);
        }
        return dp[end+1];
    }
}
 
 
原文地址:https://www.cnblogs.com/theskulls/p/5538253.html