Created
October 6, 2017 05:43
-
-
Save vetional/7963e57ed36dcd3f8e53441c42d0727e to your computer and use it in GitHub Desktop.
Max slice created by vetional - https://repl.it/MLrd/0
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
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