题目
题解
python
class Solution:
def rob(self, nums: List[int]) -> int:
N = len(nums)
# 定义状态: dp[i]表示从第i间房子开始抢劫,最多能抢到的金额
dp = [0 for i in range(N)]
for i in range(N-1, -1, -1):
if i == N-1:
dp[i] = nums[i]
elif i == N-2:
dp[i] = max(nums[i], nums[i+1])
else:
dp[i] = max(dp[i+1], nums[i] + dp[i+2])
return dp[0]