LeetCode198 打家劫舍
\(dp[i]\) 表示前 \(i\) 个房间带来的收益,这里不一定要选择第 \(i\) 个房间
class Solution:
def rob(self, nums: List[int]) -> int:
l = len(nums)
if l == 0: return 0
if l == 1: return nums[0]
dp = [0] * l
dp[0], dp[1] = nums[0], max(nums[0], nums[1])
for i in range(2, l):
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
return dp[l - 1]