# 213. House Robber II 打家劫舍 II

## # 题目描述：

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are `arranged in a circle`. That means the first house is the neighbor of the last one. Meanwhile, 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`.

Example 1:

``````Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
``````

Example 2:

``````Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
``````

## # 解题方法

``````dp[0] = num[0] （当i=0时）
dp[1] = max(num[0], num[1]) （当i=1时）
dp[i] = max(num[i] + dp[i - 2], dp[i - 1]) （当i !=0 and i != 1时）
``````

``````class Solution:
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 0
if len(nums) == 1: return nums[0]
if len(nums) == 2: return max(nums[0], nums[1])
N = len(nums)
return max(self.rob_range(nums[0 : N - 1]), self.rob_range(nums[1 : N]))

def rob_range(self, nums):
if not nums: return 0
if len(nums) == 1: return nums[0]
if len(nums) == 2: return max(nums[0], nums[1])
N = len(nums)
dp = [0] * N
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])
for i in range(2, N):
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
return dp[-1]
``````

http://www.cnblogs.com/grandyang/p/4518674.html

## # 日期

2018 年 10 月 9 日 ———— 天气骤冷，注意保暖