题解 | 不相邻取数

不相邻取数

https://www.nowcoder.com/practice/a2be806a0e5747a088670f5dc62cfa1e


n = int(input())

arr = list(map(int,input().split()))
#dp[i]以i为结束的最大子数组和
dp = [0] * n
dp[0] = arr[0]
if n >1:
    dp[1] = max(arr[0],arr[1])
for i in range(2,n):
    dp[i] =max(dp[i-2]+arr[i],dp[i-1]) 
print(max(dp))





全部评论

相关推荐

给个offer灞:校友 是不是金die
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务