# Leetcode 213 House Robber II

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.

```class Solution {
public:
int rob(vector<int>& nums) {
int a1 = 0;
int b1 = 0;
int a2 = 0;
int b2 = 0;
if(nums.size() == 1) return nums[0];
for(int i = 0; i < nums.size(); i++)
{
if(i != nums.size() - 1)
{
if(i & 1) a1 = max(a1 + nums[i], b1);
else b1 = max(b1 + nums[i], a1);
}
if(i != 0)
{
if(i & 1) a2 = max(a2 + nums[i], b2);
else b2 = max(b2 + nums[i], a2);
}
}
return max(max(a1, b1), max(a2, b2));
}
};```

0 条评论

## 相关文章

### 聊聊storm的maxSpoutPending

storm-2.0.0/storm-client/src/jvm/org/apache/storm/Config.java

2205

### 温故而知新:设计模式之原型模式(Prototype)

2395

１。Webservice中的方法重载问题 （１）在要重载的WebMethod上打个MessageName标签 比如: [WebMethod(Message...

19510

721

1373

3926

### CodeForces 157A Game Outcome

A. Game Outcome time limit per test 2 seconds memory limit per test 256 me...

3457

893

### java应用CAS

CAS（Compare and Swap），即比较并替换。jdk里的大量源码通过CAS来提供线程安全操作，比如AtomicInteger类。下面我们来分析一...

1313

### LeetCode <dp>62&63.Unique Paths I&II

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the...

1584