36 ms Python Dynamic Programming


  • 2
    B
    class Solution(object):
        def rob(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if len(nums) == 0:
                return 0
            total = [0]*(len(nums))
            for i in range (len(nums)):
                if i == 0:
                    total[0] = nums[0]
                elif i == 1:
                    total[1] = nums[1]
                elif i == 2:
                    total[2] = nums[0] + nums[2]
                else:
                    total[i] = max(total[i-2], total[i-3]) + nums[i]
            return max(total[-1],total[len(total)-2])

Log in to reply
 

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