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