题解 | #打家劫舍(一)#
打家劫舍(一)
https://www.nowcoder.com/practice/c5fbf7325fbd4c0ea3d0c3ea6bc6cc79
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param nums int整型一维数组
# @return int整型
#
class Solution:
def rob(self , nums: List[int]) -> int:
# write code here
n = len(nums)
res= 0
dp =[0]*(n+1)
dp[0]=0
dp[1]= nums[0]
for i in range(2,n+1):
dp[i] =max(dp[i-1],dp[i-2]+nums[i-1])
return dp[n]
老板电器公司氛围 197人发布