Another python solution

  • 0
    class Solution(object):
        def rob(self, nums):
            :type nums: List[int]
            :rtype: int
            if len(nums) < 2:
                return sum(nums)
            return max(nums[0] + self.helper(nums[:-1], 2, {}), self.helper(nums, 1, {}))
        def helper(self, nums, i, memo):
            if i >= len(nums):
                return 0
            if i not in memo:
                memo[i] = max(nums[i] + self.helper(nums, i+2, memo), self.helper(nums, i+1, memo))
            return memo[i]

Log in to reply

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