Skip to content

Instantly share code, notes, and snippets.

@miku
Last active August 29, 2015 13:57
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 miku/9460410 to your computer and use it in GitHub Desktop.
Save miku/9460410 to your computer and use it in GitHub Desktop.
kadane
#!/usr/bin/env python
def max_subarray(A):
max_ending_here = max_so_far = 0
for x in A:
max_ending_here = max(0, max_ending_here + x)
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
if __name__ == '__main__':
A = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
print(A)
x = max_subarray(A)
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment