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.
The first one is just a simple DP problem.
public int rob(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
int len = nums.length;
if (len == 1)
return nums[0];
int[] sum = new int[len];
sum[0] = nums[0];
sum[1] = Math.max(nums[0], nums[1]);
for (int i = 2; i < len; i++) {
sum[i] = Math.max(sum[i - 1], sum[i - 2] + nums[i]);
}
return sum[len - 1];
}
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.
The second one, now the first house is connected with the last house, which means one of the house cannot be robbed. Then we can find the max money we can get if we rob nums[0] to nums[len - 2] and that we can get if nums[1] to nums[len - 1] are robbed.
public int rob(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
int len = nums.length;
if (len == 1)
return nums[0];
int[] sum = new int[len];
sum[0] = nums[0];
sum[1] = Math.max(nums[0], nums[1]);
return Math.max(max(nums, 0, len - 2), max(nums, 1, len - 1));
}
private int max(int[] nums, int start, int end) {
if (start == end)
return nums[start];
int len = end - start + 1;
int[] sum = new int[len];
sum[0] = nums[start];
sum[1] = Math.max(nums[start], nums[start + 1]);
for (int i = 2; i < len; i++) {
sum[i] = Math.max(sum[i - 1], sum[i - 2] + nums[start + i]);
}
return sum[len - 1];
}
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 1:
3
/ \
2 3
\ \
3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
3
/ \
4 5
/ \ \
1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.
The third one is a tree like structure. Consider a tree of two level, either the two leaves are robbed, or the root is robbed. Then we can recursively find the max money of robbing two leaves or root + leaves children.
public int rob(TreeNode root) {
return robHelper(root)[0];
}
private int[] robHelper(TreeNode root) {
int[]sum = {0, 0};
if (root == null) {
return sum;
}
int[]sum_left = robHelper(root.left);
int[]sum_right = robHelper(root.right);
sum[1] = sum_left[0] + sum_right[0];
sum[0] = Math.max(sum[1], sum_left[1] + sum_right[1] + root.val);
return sum;
}