[LeetCode]House Robber II
House Robber II
动态规划
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def rob_helper(start, end):
rob, not_rob = 0, 0
for i in range(start, end+1):
cur_rob = not_rob + nums[i]
not_rob = max(not_rob, rob)
rob = cur_rob
return max(rob, not_rob)
if not nums:
return 0
elif len(nums) <= 2:
return max(nums)
return max(rob_helper(1, len(nums)-1), rob_helper(0, len(nums)-2))
关注公众号:数据结构与算法那些事儿,每天一篇数据结构与算法

浙公网安备 33010602011771号