Created
November 4, 2019 13:20
-
-
Save imedadel/b2f485832a7bd868bf20c80347c6a5c7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Create an array to track the max sum | |
# The max could be either the current value, the current max, or the current value plus the max of arr[i-2] | |
def maxSubsetSum(arr): | |
maxTop = max(arr[0], arr[1]) | |
maxes = [arr[0], maxTop] | |
for i in range(2, len(arr)): | |
localMax = max(max(arr[i], maxTop), maxes[i-2]+arr[i]) | |
maxes.append(localMax) | |
maxTop = max(maxTop, localMax) | |
return maxTop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment