求数组的子序列的和最大值,复杂度 nlog(n)

#include<stdio.h>
#include<stdlib.h>
int maxtmp(int, int, int );
int max(int a[], int lower, int hight)
{
if(lower == hight){
return a[lower];
}


int mid = (hight - lower)/2+lower;
int lmax= max(a, lower, mid);
int hmax= max(a, mid+1, hight);

int lsum=a[mid], temlsum= a[mid];
int i =mid-1;
for(;i>=lower; i--){

temlsum += a[i];
if( temlsum > lsum){ lsum = temlsum;}
}
int hsum=a[mid+1],temhsum= a[mid+1];
i = mid+2;
for(; i < hight; i++){


temhsum += a[i];
if( hsum < temhsum){ hsum = temhsum;}
}


return maxtmp(hsum+lsum,lmax,hmax);


}


int maxtmp(int max, int lmax , int hmax){


if(max > lmax  && max > hmax){ return max;}
if( lmax > max  &&  lmax > hmax ){ return lmax;}
return hmax;


}
int main(){


int ar[] = {2,3,-4,-5,1,2,4};
int size = (sizeof(ar)/ sizeof(int) -1);
int a = max(ar, 0 ,size);
printf("a is:%d\n",a);
return 1;


}

你可能感兴趣的:(求数组的子序列的和最大值,复杂度 nlog(n))