Участник:Turk0v/House Robber II

Материал из DISCOPAL
Перейти к: навигация, поиск
class Solution:
    def help_func(self, nums: List[int]) -> int:
        tmp = len(nums)
        if tmp == 0: return 0
        elif tmp == 1: return nums[0]
        elif tmp == 2: return max(nums[0],nums[1])
        elif tmp == 3: return max(nums[0] + nums[2], nums[1])
        res = [0] * len(nums)
        res[0] = nums[0]
        res[1] = max(nums[0], nums[1])
 
        for i in range(2, len(nums)):
            res[i] = max(res[i-2] + nums[i], res[i-1])
        return res[-1] 
    def rob(self, nums: List[int]) -> int:
 
        tmp = len(nums)
        if tmp == 0: return 0
        elif tmp == 1: return nums[0]
        elif tmp == 2: return max(nums[0], nums[1])
        elif tmp == 3: return max(nums[0], nums[1], nums[2])
 
        return max(self.help_func(nums[:-1]), self.help_func(nums[1:]))