LeetCode-213. House Robber II (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 arearranged 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 tonightwithout alerting the police.

House Robber题目类似,只是这次有了环形

加上条件的DP问题需要进行一定的转化才能成为标准的DP问题,这个转化就成了关键。

这里的关键就是最后那一个房间N和第一个房间相连了,可以这么进行转化:

考虑抢劫了第0个房间,那么问题就是求抢劫第0~N-1个房间的最大数。

考虑不抢劫第0个房间,那么问题就是求抢劫第1~N个房间的最大数。

public int rob(int[] nums) {
		if (nums == null)
			return 0;
		int n = nums.length;
		if (n == 0)
			return 0;
		if (n == 1)
			return nums[0];
		return Math.max(robDP(nums, 0, n - 2), robDP(nums, 1, n - 1));
	}

	int robDP(int[] nums, int first, int last) {
		int n = last - first + 1;
		if (n == 0)
			return 0;
		if (n == 1)
			return nums[first];
		int dp[] = new int[n];
		dp[0] = nums[first];
		// 注意下标
		dp[1] = Math.max(nums[first], nums[first + 1]);
		for (int i = 2; i < n; i++)
			dp[i] = Math.max(dp[i - 2] + nums[first + i], dp[i - 1]);
		return dp[n - 1];

	}
优化:

int rob(int[] nums, int first, int last) {
		// 因为只需要比较第dp[i-2]+nums[i]和dp[i-1]的大小,
		// 所以可以把House Robber I中的数组也省略掉,
		// 而只需要两个变量来存储着两个值,这样更能节约空间。
		// 这里pLast存储的是House Robber I中dp[i-1]的值,
		// ppLast存储的是House Robber I中dp[i-2]的值,
		//逻辑和House Robber I是一样的。
		int pLast = 0, ppLast = 0;
		for (int i = first; i <= last; i++) {
			int tmp = pLast;
			pLast = Math.max(ppLast + nums[i], pLast);
			ppLast = tmp;
		}
		return pLast;

	}

参考: LeetCode-198. House Robber (JAVA)寻找数组不相邻组合最大值DP




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值