# LeetCode House Robber II

Description:

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.

Solution:

import java.util.*;

public class Solution {
public int rob(int[] nums) {
int n = nums.length;
if (n == 0)
return 0;
int dp[][] = new int[n][2];

if (n == 1)
return nums[0];

dp[0][0] = 0;
dp[0][1] = nums[0];

dp[1][0] = 0;
dp[1][1] = nums[1];
for (int i = 2; i < n; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = dp[i - 1][0] + nums[i];
}
int max = Math.max(dp[n - 1][0], dp[n - 1][1]);

dp[1][0] = nums[0];
dp[1][1] = nums[1];
for (int i = 2; i < n; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = dp[i - 1][0] + nums[i];
}
max = Math.max(max, dp[n - 1][0]);

return max;
}
}

#### leetcode 213 : House Robber II

2015-05-21 07:36:47

#### LeetCode213:House Robber II

2015-06-13 11:14:36

#### leetcode之House Robber(打家劫舍)

2015-11-05 20:24:01

#### 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] 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

#### 198. House Robber Leetcode Python

2015-03-31 22:28:07