willsun888
10/12/2013 - 12:54 PM

求数组的子数组之和的最大值

求数组的子数组之和的最大值

int MaxSum(int* arr,int n){
    int start = arr[n-1];
    int all = arr[n-1];
    for(int i=n-2;i>=0;i--){
        start = max(arr[i],start+arr[i]);
        all = max(start,all);
    }
    return all;
}

int MaxSum(int* arr,int n){
    int start = arr[n-1];
    int all = arr[n-1];
    for(int i=n-2;i>=0;i--){
        if(start <0)
            start =0;
        start += arr[i];
        if(start > all)
            all = start;
    }
    return all;
}