首页 > 其他 > 详细

leetcode-198-打家劫舍

时间:2019-07-20 19:59:22      阅读:86      评论:0      收藏:0      [点我收藏+]

题目描述:

技术分享图片

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

 

leetcode-198-打家劫舍

原文:https://www.cnblogs.com/oldby/p/11218850.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!