House Robber Series

House Robber

Question

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.

Analysis

利用两个变量rob,not rob 记录当前房间是否rob情况下的最大收益

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Solution {
public int rob(int[] nums) {
int len=nums.length;
int notrob=0;
int rob=0;
for(int i=0;i<len;i++){
int currob=notrob+nums[i];
notrob=Math.max(notrob,rob);
rob=currob;
}
return Math.max(notrob,rob);
}
}

House Robber II

Question

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 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.

Analysis

与第一题不同的是:此时所有房子构成一个环

分为两种情况:

  • 抢第一个房子,则与其相邻的两个房子均不可抢,考虑[2,len-2]的房子的最大值
  • 不抢第一个房子,考虑[1,len-1]房子情况下的最大值

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {
public int rob(int[] nums) {
int len=nums.length;
if(len==0) return 0;
if(len==1) return nums[0];
return Math.max(helper(nums,2,len-2)+nums[0],helper(nums,1,len-1));
}
public int helper(int[] nums, int start, int end){
int rob=0;
int notrob=0;
for(int i=start;i<=end;i++){
int currob=notrob+nums[i];
notrob=Math.max(notrob,rob);
rob=currob;
}
return Math.max(rob,notrob);
}
}

House Robber III

Question

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.

Analysis

注意:不要天真的一位是层序遍历求和然后判断!

不一定取或不取一层中的所有节点

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int rob(TreeNode root) {
int[] res=new int[2];
res=helper(root);
return Math.max(res[0],res[1]);
}
public int[] helper(TreeNode root){
int[] tmp=new int[2];
if(root==null)
return tmp;
int[] left=helper(root.left);
int[] right=helper(root.right);
tmp[0]=Math.max(left[0],left[1])+Math.max(right[0],right[1]);
tmp[1]=left[0]+right[0]+root.val;
return tmp;
}
}