You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FIND-MAXIMUM-SUBARRAY(A, low, high)
max = A[low]
j-max = A[low]
left = right = low
j-left = j-right = low
for j = low + 1 to high
sum = j-max + A[j]
if sum > j-max
j-max = sum
j-right = j
else
j-max = 0
j-left = j + 1
if j-max > max
max = j-max
left = j-left
right = j-right
return(left, right, max)
这个算法有一点问题
如果遇到负数就抛弃前面所有的和,遇到类似这样的序列
{0,13,-1,2}
就会出问题。所以应该是前面所有的和加起来小于零了才抛弃,有最大值就更新最大值。
这个算法应该是对的:
The text was updated successfully, but these errors were encountered: