原题内容
题目链接:https://leetcode-cn.com/problems/house-robber/
解决方案
class Solution:
def rob(self, nums: List[int]) -> int:
"""
动态规划 + 滚动数组
关键在于 滚动数组的理解
"""
if not nums:
return 0
size = len(nums)
if size == 1:
return nums[0]
first, second = nums[0], max(nums[0], nums[1])
for i in range(2, size):
# 滚动数组 对比 第一位 + 第三位 和 第二位值对比
first, second = second, max(first + nums[i], second)
return second
参考资源
Leetcode官方题解 https://leetcode-cn.com/problems/house-robber/solution/da-jia-jie-she-by-leetcode-solution/