Skip to content

Instantly share code, notes, and snippets.

@xeonqq
Created August 31, 2015 11:21
Show Gist options
  • Save xeonqq/9e574be445047c1d9670 to your computer and use it in GitHub Desktop.
Save xeonqq/9e574be445047c1d9670 to your computer and use it in GitHub Desktop.
In a given array, find the subset of maximal sum in which the distance between consecutive elements is at most 6.
# you can use print for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
n = len(A)
MIN_INT = -n * 20000
v = [A[0]]+(n-1)*[MIN_INT]
for i in xrange(1, n):
for j in xrange(1, 7):
if j <= i:
v[i] = max((v[i-j] + A[i]), v[i])
return v[n-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment