###### fantasia的博客

Somewhere on this planet

### 198. House Robber

Total Accepted: 71597 Total Submissions: 206073 Difficulty: Easy

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

public class Solution {
public int rob(int[] nums) {
if(nums==null || nums.length==0) return 0;
if(nums.length==1) return nums[0];
if(nums.length==2) return nums[1]>nums[0] ? nums[1] : nums[0];

int[] res = new int[nums.length];
boolean[] flag = new boolean[nums.length];
// 初始化
res[0]=nums[0]; flag[0]=true;
if(nums[1]>nums[0]){
res[1]=nums[1]; flag[1]=true;
}else{
res[1]=nums[0]; flag[1]=false;
}
// 初始化完
for(int i=2; i<res.length; i++){
if(flag[i-1]==true){
int temp=res[i-2]+nums[i];
if(temp>res[i-1]){
flag[i]=true;
res[i]=temp;
}else{
flag[i]=false;
res[i]=res[i-1];
}
}else{
res[i]=res[i-1]+nums[i];
flag[i]=true;
}
}
return res[res.length-1];
}
}

public class Solution {
public int rob(int[] num) {
if (num==null || num.length==0) return 0;
int maxToPos[] = new int[num.length+1]; //the maximum amount to the i'th house
maxToPos[0] = 0;
maxToPos[1] = num[0];
for (int i=2; i<maxToPos.length; i++){
maxToPos[i] = Math.max(maxToPos[i-1],maxToPos[i-2]+num[i-1]);
}
return maxToPos[maxToPos.length-1];
}
}

### 213. House Robber II

Total Accepted: 29523 Total Submissions: 95288 Difficulty: Medium

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

public class Solution {
public int rob(int[] num) {
if (num==null || num.length==0) return 0;
if (num.length==1) return num[0];
int maxToPos[] = new int[num.length];
int max1 = 0;
maxToPos[0] = 0;
maxToPos[1] = num[0];
for (int i=2; i<=maxToPos.length-1; i++){
maxToPos[i] = Math.max(maxToPos[i-1],maxToPos[i-2]+num[i-1]); // 0, 1, 2 <-> dp 1 2 3
}
max1= maxToPos[num.length-1];

maxToPos[1] = num[1];
for (int i=2; i<=maxToPos.length-1; i++){
maxToPos[i] = Math.max(maxToPos[i-1],maxToPos[i-2]+num[i]); // 1, 2, 3 <-> dp 1 2 3
}
return Math.max(max1, maxToPos[num.length-1]);
}
}

### 337. House Robber III

Total Accepted: 12749 Total Submissions: 34058 Difficulty: Medium

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.

/**
* 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[] num = dfs(root);
return Math.max(num[0], num[1]);
}
private int[] dfs(TreeNode x) {
if (x == null) return new int[2];
int[] left = dfs(x.left);
int[] right = dfs(x.right);
int[] res = new int[2];
res[0] = left[1] + right[1] + x.val;
res[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
return res;
}
}

#### [leetcode] 337. House Robber III

2016-03-12 20:20:56

#### [leetcode-337]House Robber III(java)

2016-03-27 22:19:41

#### leetcode 337. House Robber III-动态规划|Java|Python简洁高效

2016-03-13 21:06:45

#### LeetCode337. House Robber III

2016-03-23 20:34:40

#### LeetCode213:House Robber II

2015-06-13 11:14:36

#### leetcode 213 : House Robber II

2015-05-21 07:36:47

#### 198. House Robber [easy] (Python)

2016-06-01 14:39:22

#### leetCode198-House Robber

2015-04-01 18:28:58

#### Leetcode 198: House Robber

2015-04-01 02:51:09

#### Leetcode 213 House Robber II 抢劫最大金额

2015-08-19 23:38:10

## 不良信息举报

Leetcode 198. House Robber & 213. House Robber II & 337. House Robber III