Leetcode 213 Solution
This article provides solution to leetcode question 213 (house-robber-ii)
Access this page by simply typing in "lcs 213" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
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:]))