Skip to content

Instantly share code, notes, and snippets.

@vetional
Created October 6, 2017 05:43
Show Gist options
  • Save vetional/7963e57ed36dcd3f8e53441c42d0727e to your computer and use it in GitHub Desktop.
Save vetional/7963e57ed36dcd3f8e53441c42d0727e to your computer and use it in GitHub Desktop.
Max slice created by vetional - https://repl.it/MLrd/0
from functools import reduce
def golden_max_slice(A):
max_ending = max_slice = 0
if all(a > 0 for a in A):
return reduce((lambda x, y: x + y), A)
if all(a < 0 for a in A):
return max(A)
for a in A:
max_ending = max(0, max_ending + a)
if max_ending > max_slice:
max_slice = max_ending
print (a, max_slice)
return max_slice
A = [5, 2, -7, 3, 5, -2, 4, -1, 6]
print(golden_max_slice(A))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment