子数组之和的最大值

  1. 子数组/子序列,而不是子集合

  1. //感谢网友firo  
  2. //July、2010.06.05。  
  3.   
  4. //Algorithm 1:时间效率为O(n*n*n)  
  5. int MaxSubsequenceSum1(const int A[],int N)  
  6. {  
  7.     int ThisSum=0 ,MaxSum=0,i,j,k;  
  8.     for(i=0;i<N;i++)  
  9.         for(j=i;j<N;j++)  
  10.         {  
  11.             ThisSum=0;  
  12.             for(k=i;k<j;k++)  
  13.                 ThisSum+=A[k];  
  14.               
  15.             if(ThisSum>MaxSum)  
  16.                 MaxSum=ThisSum;  
  17.         }  
  18.         return MaxSum;  
  19. }  
  20.   
  21. //Algorithm 2:时间效率为O(n*n)  
  22. int MaxSubsequenceSum2(const int A[],int N)  
  23. {  
  24.     int ThisSum=0,MaxSum=0,i,j,k;  
  25.     for(i=0;i<N;i++)  
  26.     {  
  27.         ThisSum=0;  
  28.         for(j=i;j<N;j++)  
  29.         {  
  30.             ThisSum+=A[j];  
  31.             if(ThisSum>MaxSum)  
  32.                 MaxSum=ThisSum;  
  33.         }  
  34.     }  
  35.     return MaxSum;  
  36. }  
  37.   
  38. //Algorithm 3:时间效率为O(n*log n)  
  39. //算法3的主要思想:采用二分策略,将序列分成左右两份。  
  40. //那么最长子序列有三种可能出现的情况,即  
  41. //【1】只出现在左部分.  
  42. //【2】只出现在右部分。  
  43. //【3】出现在中间,同时涉及到左右两部分。  
  44. //分情况讨论之。  
  45. static int MaxSubSum(const int A[],int Left,int Right)  
  46. {  
  47.     int MaxLeftSum,MaxRightSum;              //左、右部分最大连续子序列值。对应情况【1】、【2】  
  48.     int MaxLeftBorderSum,MaxRightBorderSum;  //从中间分别到左右两侧的最大连续子序列值,对应case【3】。  
  49.     int LeftBorderSum,RightBorderSum;  
  50.     int Center,i;  
  51.     if(Left == Right)Base Case  
  52.         if(A[Left]>0)  
  53.             return A[Left];  
  54.         else  
  55.             return 0;  
  56.         Center=(Left+Right)/2;  
  57.         MaxLeftSum=MaxSubSum(A,Left,Center);  
  58.         MaxRightSum=MaxSubSum(A,Center+1,Right);  
  59.         MaxLeftBorderSum=0;  
  60.         LeftBorderSum=0;  
  61.         for(i=Center;i>=Left;i--)  
  62.         {  
  63.             LeftBorderSum+=A[i];  
  64.             if(LeftBorderSum>MaxLeftBorderSum)  
  65.                 MaxLeftBorderSum=LeftBorderSum;  
  66.         }  
  67.         MaxRightBorderSum=0;  
  68.         RightBorderSum=0;  
  69.         for(i=Center+1;i<=Right;i++)  
  70.         {  
  71.             RightBorderSum+=A[i];  
  72.             if(RightBorderSum>MaxRightBorderSum)  
  73.                 MaxRightBorderSum=RightBorderSum;  
  74.         }  
  75.         int max1=MaxLeftSum>MaxRightSum?MaxLeftSum:MaxRightSum;  
  76.         int max2=MaxLeftBorderSum+MaxRightBorderSum;  
  77.         return max1>max2?max1:max2;  
  78. }  
  79.   
  80. //Algorithm 4:时间效率为O(n)  
  81. //同上述第一节中的思路3、和4。  
  82. int MaxSubsequenceSum(const int A[],int N)  
  83. {  
  84.     int ThisSum,MaxSum,j;  
  85.     ThisSum=MaxSum=0;  
  86.     for(j=0;j<N;j++)  
  87.     {  
  88.         ThisSum+=A[j];  
  89.         if(ThisSum>MaxSum)  
  90.             MaxSum=ThisSum;  
  91.         else if(ThisSum<0)  
  92.             ThisSum=0;  
  93.     }  
  94.     return MaxSum;  
  95. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值