213. House Robber II
Adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
def rob(nums: List[int]):
def linear_rob(houses):
H = len(houses)
max_amounts = [0]*3
max_amount = 0
for i in range(H):
j = (i + 3) % 3
max_amounts[j] = max(max_amounts[j-2] + houses[i], max_amounts[j-3] + houses[i])
max_amount = max(max_amount, max_amounts[j])
return max_amount
if len(nums) == 0 or nums is None:
return 0
if len(nums) == 1:
return nums[0]
return max(linear_rob(nums[:-1]), linear_rob(nums[1:]))
Comments
Post a Comment