Question:

http://www.lintcode.com/en/problem/house-robber

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.

Answer:

time:O(N)

```
class Solution {
public:
/*
* @param A: An array of non-negative integers
* @return: The maximum amount of money you can rob tonight
*/
long long houseRobber(vector<int> &A) {
size_t as = A.size();
if (as == 0)
return 0;
if (as == 1)
return A[0];
if (as == 2)
return max(A[0], A[1]);
vector<long long> dp(as);
dp[0] = A[0];
dp[1] = max(A[0], A[1]);
for (size_t i = 2; i < as; i++)
dp[i] = max(dp[i-1], dp[i-2] + A[i]);
return dp[as-1];
}
};
```

time:O(N)

memory:O(1)

```
class Solution {
public:
/*
* @param A: An array of non-negative integers
* @return: The maximum amount of money you can rob tonight
*/
long long houseRobber(vector<int> &A) {
size_t as = A.size();
long long odd, even;
odd = even = 0;
for (size_t i = 0; i < as; i++)
if (i % 2)
odd = max(odd+A[i], even);
else
even = max(even + A[i], odd);
return max(odd, even);
}
};
```