Skip to content

Instantly share code, notes, and snippets.

@foxish
Created October 29, 2013 09:33
Show Gist options
  • Select an option

  • Save foxish/7211540 to your computer and use it in GitHub Desktop.

Select an option

Save foxish/7211540 to your computer and use it in GitHub Desktop.
Kadane's algorithm, along with finding sum-array bounds
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