LeetCode213 打家劫舍 II
作者:互联网
考虑环形,首位不能同时选择
则分别对 \([0, len - 2]\) 和 \([1, len - 1]\) 做LeetCode198 打家劫舍
class Solution:
def rob(self, nums: List[int]) -> int:
def solver(start: int, end: int) -> int:
first = nums[start]
second = max(nums[start], nums[start + 1])
for i in range(start + 2, end + 1):
first, second = second, max(first + nums[i], second)
return second
length = len(nums)
if length == 1:
return nums[0]
elif length == 2:
return max(nums[0], nums[1])
else:
return max(solver(0, length - 2), solver(1, length - 1))
标签:return,nums,int,length,II,start,second,LeetCode213,打家劫舍 来源: https://www.cnblogs.com/solvit/p/16425410.html