【Leetcode】213. House Robber II

【Leetcode】213. House Robber II

class Solution(object):
    """
    The helprob function is what we used in House RobberI , and we can reuse it again.
    There is a cycle means that we can't rob the start and the end house simultaneously so the must be a house which is robbed
    In this case we only need to do house rob I in the left houses
    """
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) == 0: return 0
        if len(nums) == 1: return nums[0]
        return max(self.helprob(nums[1:]), self.helprob(nums[:-1]))


    def helprob1(self, nums):
        if len(nums) ==0 :return 0
        rob = [0]* len(nums)
        no_rob = [0]*len(nums)
        rob[0], no_rob[0] = nums[0], 0
        for i in range(1, len(nums)):
            rob[i] = nums[i] + no_rob[i-1]
            no_rob[i] = max(rob[i-1], no_rob[i-1])
        return max(rob[-1], no_rob[-1])

    def helprob(self, nums):
        if len(nums) == 0: return 0
        dp = [0] * len(nums)
        dp[0] = nums[0]
        for i in range(1,len(nums)):
            dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
        return dp[-1]

x =Solution()
print(x.rob([0,0]))