Max double slice sum

前端 未结 14 1196
后悔当初
后悔当初 2020-12-13 20:53

Recently, I tried to solve the Max Double Slice Sum problem in codility which is a variant of max slice problem. My Solution was to look for a slice that has maximum value w

14条回答
  •  佛祖请我去吃肉
    2020-12-13 20:55

    Hello this implementacion has 100 score

    int i,n ;
    
    n = A.size();
    
    if (3==n) return 0;
    
    vector  max_sum_end(n,0);
    vector  max_sum_start(n,0);
    
    for (i=1; i< (n-1); i++) // i=0 and i=n-1 are not used because x=0,z=n-1
    {
      max_sum_end[i]   = max ( 0 , max_sum_end[i-1] + A[i]  ); 
    }
    
    for (i=n-2; i > 0; i--) // i=0 and i=n-1 are not used because x=0,z=n-1
    {
       max_sum_start[i]   = max ( 0 , max_sum_start[i+1] + A[i]  ); 
    }  
    
    int maxvalue,temp;
    maxvalue = 0;
    
    for (i=1; i< (n-1); i++)
    {
     temp = max_sum_end[i-1]  + max_sum_start[i+1];
     if ( temp >  maxvalue) maxvalue=temp;
    }
    
    return maxvalue ;
    

提交回复
热议问题