题目:
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.
题意:
给定一个非负整数数组表示一排房子。每个数表示房子里的金钱数。一个小偷要偷最多的钱,但是同时进入相邻的两个房子会引发警报。
求小偷能偷的钱最大值。
方法一:性能35ms
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ln = len(nums)
retDp = [0 for each in nums]
if ln == 0:
return 0
elif ln == 1:
return nums[0]
elif ln == 2:
return max(nums[0], nums[1])
else:
retDp[0] = nums[0]
retDp[1] = max(nums[0], nums[1])
for i in range(2,ln):
retDp[i] = max(retDp[i-1], retDp[i-2] + nums[i])
return retDp[-1]