# 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

leetcode 213 : House Robber II
• xudli
• 2015年05月21日 07:36
• 9546

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

HouseRobber扩展：抢劫完那条街道上的房子之后，这个贼有找到了实施自己盗窃行动的另一个地点，这样他才不会引来太多注意。这次，这里的搜有房子排成了一个圆圈，这意味着第一个房子和最后一个房子也是挨...
• smile_watermelon
• 2015年08月19日 23:38
• 557

## LeetCode213:House Robber II

Note: This is an extension of House Robber. After robbing those houses on that street, the thief ...
• u012501459
• 2015年06月13日 11:14
• 1210

## leetCode198-House Robber

• Lu597203933
• 2015年04月01日 18:28
• 7397

## Leetcode_198_House Robber

• pistolove
• 2015年08月15日 12:02
• 2105

## 198. House Robber Leetcode Python

You are a professional robber planning to rob houses along a street. Each house has a certain amount...
• hyperbolechi
• 2015年03月31日 22:28
• 1258

## 【LeetCode-面试算法经典-Java实现】【198-House Robber（抢劫犯）】

【189-House Robber（抢劫犯）】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-Chao】原题　　Y...
• DERRANTCM
• 2015年08月25日 06:41
• 3784

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

• zdavb
• 2016年03月27日 22:19
• 825

## Leetcode 198: House Robber

House Robber Total Accepted: 642 Total Submissions: 2562You are a professional robber planning to r...
• xudli
• 2015年04月01日 02:51
• 4437

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

• coder_orz
• 2016年06月01日 14:39
• 1369

举报原因： 您举报文章：LeetCode House Robber II 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)