House Robber I & II & III
House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that 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.
Given [3, 8, 4]
, return 8
.
分析:
因为“偷”第一个或者“偷”第二个对后面的选择是有影响的,所以从后往前推算更好。
total[i] = Math.max(A[i] + total[i + 2], total[i + 1]);
A[i] + total[i + 2] 指的是偷第i家。
total[i + 1] 指的是不偷i家。
1 public class Solution { 2 /** 3 * @param A: An array of non-negative integers. 4 * return: The maximum amount of money you can rob tonight 5 */ 6 public long houseRobber(int[] A) { 7 if (A == null || A.length == 0) return 0; 8 if (A.length == 1) return A[0]; 9 if (A.length == 2) return Math.max(A[0], A[1]); 10 11 long[] total = new long[A.length]; 12 int length = total.length; 13 total[length - 1] = A[length - 1]; 14 total[length - 2] = Math.max(A[length - 1], A[length - 2]); 15 16 for (int i = length - 3; i >= 0; i--) { 17 total[i] = Math.max(total[i + 2] + A[i], total[i + 1]); 18 } 19 return total[0]; 20 } 21 }
House Robber II
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.
nums = [3,6,4]
, return 6.
分析:
现在是一个环了,感觉好像找不到起始点。其实反过来想,就是头尾不能同时选而已。所以我们分别选取两个不同的起始点和结束点,跑两次就可以了。
1 public class Solution { 2 3 public int houseRobber2(int[] nums) { 4 if (nums == null || nums.length == 0) return 0; 5 if (nums.length == 1) return nums[0]; 6 return Math.max(getMax(nums, 0, nums.length - 2), getMax(nums, 1, nums.length - 1)); 7 } 8 9 public int getMax(int[] nums, int start, int end) { 10 if (start == end) return nums[start]; 11 if (start + 1 == end) return Math.max(nums[start], nums[end]); 12 13 int[] total = new int[end - start + 1]; 14 int m = total.length; 15 total[m - 1] = nums[end]; 16 total[m - 2] = Math.max(nums[end], nums[end - 1]); 17 18 for (int i = m - 3; i >= 0; i--) { 19 total[i] = Math.max(nums[end - (m - i) + 1] + total[i + 2], total[i + 1]); 20 } 21 return total[0]; 22 } 23 }
House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
Example
3
/ \
2 3
\ \
3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
3
/ \
4 5
/ \ \
1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.
分析:
看到树,想都不用多想,立马想到递归。关键是这个递归怎么写啊?
既然我们不知道是否把root包含进去是否是最优,那么我们就创建一个函数,返回一个数组,这个数组包含两种情况下的最大值。
1 public class Solution { 2 3 public int rob(TreeNode root) { 4 if(root == null) return 0; 5 6 int[] result = helper(root); 7 return Math.max(result[0], result[1]); 8 } 9 10 public int[] helper(TreeNode root){ 11 if(root == null){ 12 return new int[]{0, 0}; 13 } 14 15 int[] result = new int[2]; 16 int[] left = helper(root.left); 17 int[] right = helper(root.right); 18 19 // result[0] is when root is selected, result[1] is when not. 20 result[0] = root.val + left[1] + right[1]; 21 result[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); 22 23 return result; 24 } 25 }