Eln*_*naz 5 c arrays algorithm
问题是 :
通过选择元素来找到正整数数组中可能的最大总和,使得没有两个元素彼此相邻.
有一个这样的答案:但这个问题的最佳答案是什么
让我们用"t"表示数组并从0开始索引.设f是一个函数,使得f(k)= [0..k]子阵列中的最大和与问题的条件.现在使用动态编程:
f(0) = t[0]
f(1) = max{ t[0], t[1] }
f(k) = max{ f(k-2) + t[k], f(k-1) } if k >= 2
If the array has n elements we need f(n-1).
Run Code Online (Sandbox Code Playgroud)
提前致谢.