Leetcode 213 Solution

This article provides solution to leetcode question 213 (house-robber-ii)

https://leetcode.com/problems/house-robber-ii

Solution

class Solution:
    def rob(self, nums: List[int]) -> int:
        if not nums:
            return 0

        def _rob(nums):
            if len(nums) == 0:
                return 0
            elif len(nums) == 1:
                return nums[0]

            a = nums[0]
            b = max(nums[0], nums[1])

            ans = max(a, b)

            for num in nums[2:]:
                c = max(num + a, b)

                a = b
                b = c

                ans = max(ans, c)
            return ans

        return max(nums[0] + _rob(nums[2:-1]), _rob(nums[1:]))