0
アルゴリズムは正しく見えますが、間違いがどこにあるのか分かりません。問題は、その範囲は8〜10のコード戻り、32の合計最大サブアレイであるが、実際の答えは43間違った出力を返す最大合計サブアレイ
import sys
import math
def maxtuple(lss,rss):
if lss[2] > rss[2]:
return lss
else:
return rss
def crosssubarray(A, start, mid, end):
ls=rs=-sys.maxsize
maxleft=0
maxright=0
sum = 0;
for i in reversed(range(start, mid)):
sum = sum + A[i]
print(i)
if sum > ls:
ls = sum
maxleft = i
sum = 0
for i in range(mid+1, end):
sum = sum+ A[i]
if sum > rs:
rs = sum
maxright = i
return (maxleft, maxright, ls+rs)
def maxsubarray(A,start,end):
if start == end:
return (start,end,A[start])
else:
mid = (start+end)/2
lss = maxsubarray(A, start, mid)
rss = maxsubarray(A, mid+1, end)
css = crosssubarray(A, start, mid, end)
maxsub = maxtuple(lss,rss)
maxall = maxtuple(maxsub, css)
return maxall`enter code here`
A = [13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7]
print(maxsubarray(A,0,15))
申し訳ありませんが、acutal出力は合計7 43 – Sivabushan