Topic: You are a professional thief to steal street plan of the house, each room are in possession of some cash. This place all the houses are in a circle, which means that the first and the last house is next to the house. Meanwhile, neighboring houses equipped with anti-theft system communicate with each other, if two adjacent houses on the same night, thieves broke into the system will automatically alarm.
Given a representative from each non-negative integer array of Housing storage amount calculated in case you do not touch the alarm device, can steal the maximum amount to.
Link: https: //leetcode-cn.com/problems/house-robber-ii
Act I: the official Code
Ideas: an annular array becomes the biggest difference from the linear array is inclusive numbers can not take the same time, so that miss the main contradiction, calculated two types, one is not to take the first number, the other is not taken The last number,
class Solution: def rob(self, nums: [int]) -> int: def my_rob(nums): cur, pre = 0, 0 for NUM in the nums: # the second maximum value to the right is cur num [i-1], it is to be assigned to pre, because the next cycle of pre cru is cur, pre = max (pre + NUM, CUR), CUR return CUR return max (my_rob (the nums [: -. 1]), my_rob (the nums [. 1:])) IF ! len (the nums) =. 1 the else the nums [0] IF the __name__ = = ' __main__ ' : duixaing = Solution() a = duixaing.rob([2,3,2]) print(a)
ttt