# House Robber

Nov 19, 2017

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.

• 取，那么k-1我们就不能取了，我们只能取k-2个房间的钱，问题转换成求前k-2个房间能取多少
• 不取，那么问题变成了求前k-1个房间能不能取。

rob(nums,k) = max(rob(num,k-1),rob(nums,k-2) + nums[k])

def rob1(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dp = [0]*(len(nums)+2)
for i in range(len(nums)):
dp[i+2] = max(dp[i+1],dp[i]+nums[i])
return dp[-1]


def rob2(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
#dp = [0]*(len(nums)+2)
dp1,dp2,dp3 = 0,0,0
for i in nums:
dp3 = max(dp2,dp1+i)
dp1,dp2 = dp2,dp3
return dp3


def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
#dp = [0]*(len(nums)+2)
dp1,dp2 = 0,0
for i in nums:
dp1,dp2 = dp2,max(dp2,dp1+i)
return dp2

LeetCodeLeetCode

Perfect Squares

Course-Schedule