[LeetCode] 213. House Robber II

原题链接: https://leetcode.com/problems/house-robber-ii/

House Robber 专题相关题目

198. House Robber
213. House Robber II
337. House Robber III

1. 题目介绍

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

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.

你是一个专业的强盗,要去一个环形街区入市抢劫。环形街区的房子排成了一个圈,第一个房子的邻居就是最后一个房子和第二个房子。相邻的房子有安全系统,如果某个房子和它相邻的房子都被盗了,报警系统就会通知警察。但是如果某个房子被盗,但是它相邻的房子没有被盗,报警系统就不会通知警察。

给出一个数组,数组中存放着第 i 个房子的钱数。
求在不通知警察的情况下,能拿走最多的钱数。

Example 1:

Input: [2,3,2]
Output: 3
Explanation: 
You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), 
because they are adjacent houses.
Example 2:

Input: [1,2,3,1]
Output: 4
Explanation: 
Rob house 1 (money = 1) and then rob house 3 (money = 3). 
Total amount you can rob = 1 + 3 = 4.

2. 解题思路

动态规划法解题

根据题意,不能够抢劫相邻的房子。由于这些房子是按照圆圈排列的,第一个房子和最后一个房子也是相邻的。如果抢劫最后一间房子,那么就不能抢劫第1间。

因此可以得出两种策略:

  1. 除去最后一间房子,在第1间房子至第n-1间房子中寻找最大值。
  2. 出去第1间房子,在第2间房子和第n间房子中寻找最大值。

分别使用198. House Robber中的方法计算每个策略的最大值,然后从这两种策略中取最大的值就可以了。

实现代码

class Solution {
    public int rob(int[] nums) {
        
		int length = nums.length;
		if(length == 0 ) {
			return 0;
		}
		if(length == 1) {
			return nums[0];
		}
		if(length == 2) {
			return Math.max(nums[0],nums[1]);
		}
		
		int[] dp1 = new int[length];
		dp1[0] = nums[0];
		dp1[1] = nums[1];
		int Max1 =  nums[0];
		
		for(int i =2 ;i<length-1;i++) {
			if(dp1[i-2] > Max1 ) {
				Max1 = dp1[i-2];
			}
			dp1[i] = Math.max(dp1[i-1], Max1 + nums[i]);
		}
		int ans1 = Math.max(dp1[length-2], dp1[length-3]);

		int[] dp2 = new int[length];
		dp2[1] = nums[1];
		dp2[2] = nums[2];
		int Max2 =  nums[1];
		
		for(int i =3 ;i<length;i++) {
			if(dp2[i-2] > Max2 ) {
				Max2 = dp2[i-2];
			}
			dp2[i] = Math.max(dp2[i-1], Max2 + nums[i]);
		}
		int ans2 = Math.max(dp2[length-1], dp2[length-2]);
		
		return Math.max(ans1, ans2);
    }
}

改进方法1

上面的代码太长了。我们可以专门写一个函数,这个函数求从第x到第y间房子的最大值,然后调用它去求两种策略的最大值。
实现代码

class Solution {
    public int rob(int[] nums) {
		int length = nums.length;
		if(length ==0 || nums == null) {
        	return 0;
        }
		if(length ==1) {
        	return nums[0];
        }
		
		int ans1 = FindMax(0,length-2,nums);
		int ans2 = FindMax(1,length-1,nums);
		
		return Math.max(ans1, ans2);
	}
	
	public int FindMax(int x,int y,int[] nums) {
		
		int length = y-x+1;
        if(length == 1) {
        	return nums[x];
        }

        int [] dp = new int[length];
        int Max = nums[x];
        dp[0] = nums[x];
        dp[1] =  nums[x+1];
        
        for(int i = 2 ;i<length ; i++) {
            if(dp[i-2] > Max){
                Max = dp[i-2];
            }
        	dp[i] = Math.max(dp[i-1] , Max + nums[i+x] ) ;
        }
        return Math.max(dp[length-1],dp[length-2]);
	}
}

改进方法2

上述动态规划的方法,状态转移方程本质上是:
d p [ i ] = m a x ( d p [ i − 1 ] , M a x + n u m s [ i ] ) dp[i] = max( dp[i-1] , Max + nums[i] ) dp[i]=max(dp[i1],Max+nums[i])

其中Max是dp[0]到dp[i-2]的最大值。

该方程实际上只用了dp[i-1]和dp[i-2]的值,因此可以将dp数组简化为三个int类型的数。

实现代码

class Solution {
    public int rob(int[] nums) {
		int length = nums.length;
		if(length ==0 || nums == null) {
        	return 0;
        }
		if(length ==1) {
        	return nums[0];
        }
		
		int ans1 = FindMax(0,length-2,nums);
		int ans2 = FindMax(1,length-1,nums);
		
		return Math.max(ans1, ans2);
	}
	
	public int FindMax(int x,int y,int[] nums) {
		
		int length = y-x+1;
        if(length == 1) {
        	return nums[x];
        }

        int a,b,c;
        int Max = 0;
        a = nums[x];
        b = nums[x+1];

        for(int i = 2;i<length;i++) {
        	if(a > Max){
                Max = a;
            }
        	c = Math.max( b  , Max + nums[i+x] ) ;
        	
        	a = b;
        	b = c;
        }
        return Math.max(a,b);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值