# dp[i]代表前i个的最大自序和
# dp[i] = max(d[i-1], dp[i-2] + array[i])
class Solution:
def subsequence(self , n , array ):
if not array:
return 0
dp = [0]*(n+1)
dp[0] = 0
dp[1] = max(dp[0], array[0])
for i in range(2, n+1):
dp[i] = max(dp[i-1], dp[i-2] + array[i-1])
return dp[-1]
不相邻最大子序列和
最新推荐文章于 2024-05-29 10:09:40 发布