Skip to content

Instantly share code, notes, and snippets.

@danielrobertson
Created March 9, 2016 16:28
Show Gist options
  • Select an option

  • Save danielrobertson/f693337f1068ca02fec0 to your computer and use it in GitHub Desktop.

Select an option

Save danielrobertson/f693337f1068ca02fec0 to your computer and use it in GitHub Desktop.
Maximum Contiguous Subarray
def maxSubarray(numbers):
currentMax = 0
totalMax = 0
for n in numbers:
currentMax = max(0, currentMax + n)
totalMax = max(totalMax, currentMax)
return totalMax
numbers = [-10, 2,2,-4,2,3,4,-5]
print(str(maxSubarray(numbers))) # 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment