Last active
August 29, 2015 13:57
-
-
Save miku/9460410 to your computer and use it in GitHub Desktop.
kadane
This file contains 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
#!/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