## 题目描述：

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.

## 解题思路：

dp[i] = max(dp[i - 1], dp[i - 2] + num[i - 1])

## Python代码：

``````class Solution:
# @param num, a list of integer
# @return an integer
def rob(self, num):
size = len(num)
dp = [0] * (size + 1)
if size:
dp[1] = num[0]
for i in range(2, size + 1):
dp[i] = max(dp[i - 1], dp[i - 2] + num[i - 1])
return dp[size]
``````

``````class Solution:
# @param num, a list of integer
# @return an integer
def rob(self, num):
size = len(num)
odd, even = 0, 0
for i in range(size):
if i % 2:
odd = max(odd + num[i], even)
else:
even = max(even + num[i], odd)
return max(odd, even)
``````

Pingbacks已关闭。

1. s 发布于 2016年1月7日 10:52 #

是不是应该是这样的？递推关系为maxV[i] = max(maxV[i-2]+num[i], maxV[i-1])

2. 书影网友 发布于 2017年4月22日 06:06 #

转移方程是不是 dp[i] = max(dp[i - 1], dp[i - 2] + num[i]） 不是➕num[i-1]