X
xianwei
int
MaxSubsequenceSum( const int A[], int N )
{
int ThisSum, MaxSum, j;
ThisSum = MaxSum = 0;
for (j = 0; j < N; j++)
{
ThisSum += A[j];
if (ThisSum > MaxSum)
{
MaxSum = ThisSum;
}
else if (ThisSum < 0)
{
ThisSum = 0;
}
}
return MaxSum;
}
I think the above code is a good choice to obtain MaxSubsequenceSum
T(n) = O(n), but If the array A contain full negative value, the
function will return zero.
I think to add a part code to test the A array value, but this will
destroy code grace, and then the code will become ugly(I think).
Do you have a good method to resolve the problem?
Thank you.
MaxSubsequenceSum( const int A[], int N )
{
int ThisSum, MaxSum, j;
ThisSum = MaxSum = 0;
for (j = 0; j < N; j++)
{
ThisSum += A[j];
if (ThisSum > MaxSum)
{
MaxSum = ThisSum;
}
else if (ThisSum < 0)
{
ThisSum = 0;
}
}
return MaxSum;
}
I think the above code is a good choice to obtain MaxSubsequenceSum
T(n) = O(n), but If the array A contain full negative value, the
function will return zero.
I think to add a part code to test the A array value, but this will
destroy code grace, and then the code will become ugly(I think).
Do you have a good method to resolve the problem?
Thank you.