Maximum Subsequence Sum Problem

Maximum Subsequence Sum Problem(最大的子序列问题) 

                                                                                                                    j

Given(possibly negative)integersA1,A2,.......An,find the maximumvalue of∑    Ak.

                                                                                                                       k=i

(For convenience, the maximum subsequence sum is 0 if all the integers are negative.)



/*
Cubic maximum contiguous subsequence sum algorithm.
*/

int maxSubSum1(const vector<int>&a)
{
 int maxSum = 0;

 for (int i = 0; i < a.size(); ++i)
 {
  for (int j = i; j < a.size(); ++j)
  {
   int thisSum = 0;

   for (int k = i; k <= j; ++k)
   {
    thisSum += a[k];
   }

   if (thisSum > maxSum)
   {
    maxSum = thisSum;
   }
  }
 }

 return maxSum;
}

/*
Quadratic maximum contiguous subsequence sum algorithm.
*/
int maxSubSum2(const vector<int>&a)
{
 int maxSum = 0;

 for (int i = 0; i < a.size(); ++i)
 {
  int thisSum = 0;
  for (int j = i; j < a.size(); ++j)
  {
   thisSum += a[j];

   if (thisSum > maxSum)
   {
    maxSum = thisSum;
   }
  }
 }

 return maxSum;
}

/*
Recursive maximum contiguous subsequence sum algorithm.
Finds maximum sum in subarray spanning a[left..right]
Does not attempt to maintain actual best sequence.
*/

int maxSumRec(const vector<int>&a, int left, int right)
{
 if (left == right)//Base case
 {
  if (a[left] > 0)
  {
   return a[left];
  }
  else
  {
   return 0;
  }
 }

 int center = (left + right) / 2;
 int maxLeftSum = maxSumRec(a, left, center);
 int maxRightSum = maxSumRec(a, center + 1, right);

 int maxLeftBorderSum = 0, leftBorderSum = 0;
 for (int i = center; i >= left; --i)
 {
  leftBorderSum += a[i];
  if (leftBorderSum > maxLeftBorderSum)
  {
   maxLeftBorderSum = leftBorderSum;
  }
 }

 int maxRightBorderSum = 0, rightBorderSum = 0;
 for (int j = center + 1; j <= right; ++j)
 {
  rightBorderSum += a[j];
  if (rightBorderSum > maxRightBorderSum)
  {
   maxRightBorderSum = rightBorderSum;
  }
 }

 return max(maxLeftSum, maxRightSum, maxLeftBorderSum + maxRightBorderSum);
}

int maxSubSum3(const vector<int>&a)
{
 return maxSumRec(a, 0, a.size() - 1);
}

/*
Linear time maximum contiguous subsequence sum algorithm.
*/

int maxSubSum4(const vector<int>&a)
{
 int maxSum = 0, thisSum = 0;

 for (int j = 0; j < a.size(); ++j)
 {
  thisSum += a[j];

  if (thisSum > maxSum)
  {
   maxSum = thisSum;
  }
  else if (thisSum < 0)
  {
   thisSum = 0;
  }
 }

 return maxSum;
}

An extra advantage of this algorithm is that it makes only one pass through the data, and once a[i] is read and processed, it does not need to be remembered. Thus, if the array is on a disk or is being transmitted over the Internet, it can be read sequentially, and there is no need to store any part of it in main memory. Furthermore, at any point in time, the algorithm can corretly give an answer to the subsequence problem for the data it has already read (the other algorithms do not share this property). Algorithms that can do this are called online algorithms. An online algorithm that requires only constant space and runs in linear time is just about as good as possible.

我自己写的一个个人小网站http://www.caozhicong.com/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值