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.
无输入输出样例
解题思路:
动态规划方程:money[i] = max(money[i-2]+nums[i],money[i-1])
加上几个临界条件
然后把一个圈分解为两条线,从1到n 和从0到n-1
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
length = len(nums)
if length == 0 :
return 0
elif length == 1 :
return nums[0]
elif length == 2 :
return max(nums[1],nums[0])
else :
ll = [0 for i in range(0,length)]
ll[0] = nums[0]
ll[1] = max(nums[0],nums[1])
a = 0
for i in range(2,length-1):
ll[i] = max(ll[i-2] + nums[i],ll[i-1])
a = max(ll)
l = [0 for i in range(0,length)]
l[1] = nums[1]
l[2] = max(nums[1],nums[2])
b = 0
for i in range(2,length):
l[i] = max(l[i-2]+nums[i],l[i-1])
b = max(l)
return max(a,b)
a = Solution()
b = [0,0,0]
c = a.rob(b)
print(c)