Skip to content

Instantly share code, notes, and snippets.

@andreydung
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreydung/76d355ed49825718ffcc to your computer and use it in GitHub Desktop.
Save andreydung/76d355ed49825718ffcc to your computer and use it in GitHub Desktop.
# Kadane's algorithm
def largest(a):
max_current = -100
maxvalue = -100
for i in range(len(a)):
if (max_current < 0):
max_current = a[i]
currenti = i
currentj = i
else:
max_current += a[i]
currentj = i
if max_current > maxvalue:
maxvalue = max_current
besti = currenti
bestj = currentj
return maxvalue, besti, bestj
print largest([-1, -2, 3, 4, -5, 6])
print largest([-7, 4, -2, 5, 3, -6, 8, -9])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment