题目描述:

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

 

相关文章:

  • 2021-11-15
  • 2021-09-15
  • 2021-12-21
  • 2021-10-01
  • 2022-12-23
猜你喜欢
  • 2021-07-20
  • 2022-01-09
  • 2021-08-04
  • 2022-12-23
  • 2021-12-12
  • 2021-09-04
相关资源
相似解决方案