After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
classSolution(object):defrob(self,nums):""" :type nums: List[int] :rtype: int """# dp[i] = max(dp[i-1], dp[i-2]+nums[i])n=len(nums)dp=[0]*(n)ifn==0:return0ifn==1:returnnums[0]ifn==2:returnmax(nums[0],nums[1])# case1: dont use dp[0]dp[0]=0dp[1]=nums[1]foriinrange(2,n):dp[i]=max(dp[i-1],dp[i-2]+nums[i])ans=dp[n-1]# case2: don't use dp[n-1]dp[0]=nums[0]dp[1]=max(nums[0],nums[1])foriinrange(2,n-1):dp[i]=max(dp[i-1],dp[i-2]+nums[i])returnmax(ans,dp[n-2])