Share Python code


  • 0
    W
    class Solution:
        # @param {integer[]} nums
        # @return {integer}
        def rob(self, nums):
            if len(nums)==0:
                return 0
            elif len(nums)==1:
                return nums[0]
            elif len(nums)==2:
                return max(nums)
            a=nums[0]
            b=max(nums[0],nums[1])
            for i in range(2,len(nums)):
                a,b=b,max(a+nums[i],b)
            return b

Log in to reply
 

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