Created
October 29, 2013 09:33
-
-
Save foxish/7211540 to your computer and use it in GitHub Desktop.
Kadane's algorithm, along with finding sum-array bounds
This file contains hidden or 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
| a = [3, -1, 5, -12, 0, -6, 3, 5, -12] | |
| end_seq = [] | |
| max_ending_here = max_so_far = n = 0 | |
| for i in range(len(a)): | |
| if max_ending_here + a[i] > a[i]: | |
| max_ending_here = max_ending_here + a[i] | |
| else: | |
| max_ending_here = a[i] | |
| n = i | |
| if max_ending_here > max_so_far: | |
| max_so_far = max_ending_here | |
| del end_seq[:] | |
| end_seq.append((n, i)) | |
| print(max_so_far, end_seq) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment