My accepted python code


  • 2
    S
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) == 0:
            return 0
        if len(nums) == 1:
            return nums[0]
        if len(nums) == 2:
            return max(nums[0],nums[1])
        
        maxrob = []
        maxrob.append(nums[0])
        maxrob.append(max(nums[0],nums[1]))
        for i in range(2,len(nums)):
            maxrob.append( max( maxrob[i-1],maxrob[i-2]+nums[i] ))
        
        return maxrob[-1]

  • 0
    H
    class Solution(object):
        def rob(self, nums):
            n = len(nums)
            s = [0,0]
            for i in range(n):
                s.append(max(s[i]+nums[i], s[i+1]))
            return s[-1]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.