标签:
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 }
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 * @param nums: An array of non-negative integers. 4 * return: The maximum amount of money you can rob tonight 5 */ 6 public int houseRobber2(int[] nums) { 7 if (nums == null || nums.length == 0) return 0; 8 if (nums.length == 1) return nums[0]; 9 return Math.max(getMax(nums, 0, nums.length - 2), getMax(nums, 1, nums.length - 1)); 10 } 11 12 public int getMax(int[] nums, int start, int end) { 13 if (start == end) return nums[start]; 14 if (start + 1 == end) return Math.max(nums[start], nums[end]); 15 16 int[] total = new int[end - start + 1]; 17 total[0] = nums[start]; 18 total[1] = Math.max(nums[start], nums[start + 1]); 19 20 for (int i = start + 2; i <= end; i++) { 21 total[i - start] = Math.max(nums[i] + total[i - start - 2], total[i - start - 1]); 22 } 23 return total[total.length - 1]; 24 } 25 }
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 /** 2 * Definition of TreeNode: 3 * public class TreeNode { 4 * public int val; 5 * public TreeNode left, right; 6 * public TreeNode(int x) { val = x; } 7 * } 8 */ 9 public class Solution { 10 /** 11 * @param root: The root of binary tree. 12 * @return: The maximum amount of money you can rob tonight 13 */ 14 public int houseRobber3(TreeNode root) { 15 if(root == null) 16 return 0; 17 18 int[] result = maxTotal(root); 19 return Math.max(result[0], result[1]); 20 } 21 22 public int[] maxTotal(TreeNode root){ 23 if(root == null){ 24 int[] result = {0, 0}; 25 return result; 26 } 27 28 int[] result = new int[2]; 29 int[] left = maxTotal(root.left); 30 int[] right = maxTotal(root.right); 31 32 // result[0] is when root is selected, result[1] is when not. 33 result[0] = root.val + left[1] + right[1]; 34 result[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); 35 36 return result; 37 } 38 }
标签:
原文地址:http://www.cnblogs.com/beiyeqingteng/p/5662136.html