Skip to content

Instantly share code, notes, and snippets.

@imedadel
Created November 4, 2019 13:20
Show Gist options
  • Save imedadel/b2f485832a7bd868bf20c80347c6a5c7 to your computer and use it in GitHub Desktop.
Save imedadel/b2f485832a7bd868bf20c80347c6a5c7 to your computer and use it in GitHub Desktop.
# 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