Skip to content

Instantly share code, notes, and snippets.

@xeonqq
Created September 6, 2015 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeonqq/9b823dd1511cb9e19eeb to your computer and use it in GitHub Desktop.
Save xeonqq/9b823dd1511cb9e19eeb to your computer and use it in GitHub Desktop.
Find the maximal sum of any double slice.
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