Created
September 6, 2015 22:34
-
-
Save xeonqq/9b823dd1511cb9e19eeb to your computer and use it in GitHub Desktop.
Find the maximal sum of any double slice.
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
def solution(A): | |
# write your code in Python 2.7 | |
n = len(A) | |
if n <=3: | |
return 0 | |
leftMax = [0]*n | |
rightMax = [0]*n | |
for i in xrange(2,n-1): | |
leftMax[i] = max(0,leftMax[i-1]+A[i-1]) | |
for i in xrange(n-3,0,-1): | |
rightMax[i] = max(0, rightMax[i+1]+A[i+1]) | |
Max = 0 | |
#print leftMax | |
#print rightMax | |
for i in xrange(n): | |
s = leftMax[i]+rightMax[i] | |
if s > Max: | |
Max=s | |
return Max |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment