int MaxSubSeq( vector coll, const unsigned int numItem)
{
int tempSum, maxSum, startPos, endPos, i, j;
i = tempSum = maxSum = 0; startPos = endPos = -1;
// here we not use the iterator,
// use the i to get position
for( j=0; j {
tempSum += coll[j];
if( tempSum > maxSum )
{
/* update maxsum, startPos, endPos */
maxSum = tempSum;
startPos = i;
endPos = j;
}
else if( tempSum < 0 )
{
i = j + 1;
tempSum = 0;
}
}
cout << "startPos= " << startPos+1
<< " endPos = " << endPos+1
<< endl;
return( maxSum );
}
阅读(1641) | 评论(0) | 转发(0) |