max sum of list elements each separated by (at least) k elements
<p>given a list of numbers to find the maximum sum of non-adjacent elements with time complexity o(n) and space complexity of o(1), i could use this :</p>

<pre><code>sum1= 0
sum2= list[0]

for i in range(1, len(list)):
    num= sum1
    sum1= sum2+ list[i]
    sum2= max(num, sum2)

print(max(sum2, sum1))
</code></pre>

<p>this code will work only if the k = 1 [ only one element between the summing numbers] how could improve it by changing k value using dynamic programming. where k is the number of elements between the summing numbers.
for example:</p>

<p>list = [5,6,4,1,2]   k=1
answer = 11 # 5+4+2</p>

<p>list = [5,6,4,1,2] k=2
answer = 8 # 6+2 </p>

<p>list = [5,3,4,10,2]   k=1
answer = 15 # 5+10</p>

<p>Not sure for the complexity but coding efficiency landed me with</p>

<pre><code>max([sum(l[i::j]) for j in range(k,len(l)) for i in range(len(l))])
</code></pre>

<p>(I've replace <code>list</code> variable by <code>l</code> not to step on a keyword).</p>

python algorithm dynamic-programming