213. House Robber II
Python
Bottom-Up DP
class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 0 or nums is None:
return 0
if len(nums) == 1:
return nums[0]
return max(
self.rob_no_cycle(nums[:-1]),
self.rob_no_cycle(nums[1:])
)
@staticmethod
def rob_no_cycle(nums: List[int]) -> int:
# Leetcode 198. House Robber
t1, t2 = 0, 0
for num in nums:
temp = t1
t1 = max(t2+num, t1)
t2 = temp
return t