题目描述:
方法一:O(N) O(N)
class Solution(object): def rob(self, nums): """ :type nums: List[int] :rtype: int """ bp = [0] * (len(nums) + 2) for i in range(len(nums)): bp[i+2] = max(bp[i+1],bp[i]+nums[i]) return bp[-1]
另:O(N)O(1)
class Solution(object): def rob(self, nums): """ :type nums: List[int] :rtype: int """ cur_max = 0 pre_max = 0 for i in nums: temp = cur_max cur_max = max(pre_max + i,cur_max) pre_max = temp return cur_max
原文:https://www.cnblogs.com/oldby/p/11218850.html