标签:indicator pen com [1] lis turn lock span type
本题和House Robber差不多,分成两种情况来解决。第一家是不是偷了,如果偷了,那么最后一家肯定不能偷。
1 class Solution(object): 2 def rob(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 if not nums: 8 return 0 9 n = len(nums) 10 if n == 1: 11 return nums[0] 12 if n == 2: 13 return max(nums[0], nums[1]) 14 15 dp = [0]* n 16 dp[0] = 0 17 dp[1] = nums[1] 18 19 for i in range(2,n): 20 dp[i] = max(dp[i-2]+nums[i], dp[i-1]) 21 22 case1 = dp[-1] 23 24 dp = [0]* (n-1) 25 dp[0] = nums[0] 26 dp[1] = nums[0] 27 28 for i in range(2,n-1): 29 dp[i] = max(dp[i-2]+nums[i], dp[i-1]) 30 31 case2 = dp[-1] 32 33 return max(case1, case2) 34
标签:indicator pen com [1] lis turn lock span type
原文地址:http://www.cnblogs.com/lettuan/p/6185507.html