Created
August 31, 2015 11:21
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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