leetcode-198-打家劫舍

题目描述:

方法一: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

 

posted @ 2019-07-20 19:05  oldby  阅读(134)  评论(0编辑  收藏  举报