Algorithm 从给定代码中查找递归函数和数学表达式

Algorithm 从给定代码中查找递归函数和数学表达式,algorithm,time-complexity,Algorithm,Time Complexity,我的任务是分析代码,以找到数学表达式、代码的递归函数和时间复杂度 守则: int M(int B[],int l,int r) { if(l == r) return (B[l]>0)?B[l]:0; int m = (l+r)/2; int mls = M(B,l,m); int mrs = M(B,m+1,r); int mlbs = 0, lbs = 0; for(int i = m;i >= l;i--){ lb

我的任务是分析代码,以找到数学表达式、代码的递归函数和时间复杂度

守则:

int M(int B[],int l,int r)
{
    if(l == r) return (B[l]>0)?B[l]:0;
    int m = (l+r)/2;
    int mls = M(B,l,m);
    int mrs = M(B,m+1,r);
    int mlbs = 0, lbs = 0;
    for(int i = m;i >= l;i--){
        lbs += B[i]; 
        if(lbs > mlbs) mlbs = lbs;
    }
    int mrbs = 0, rbs =0;
    for(int j = m+1;j <= r;j++){   
        rbs += B[j];                 
        if(rbs > mrbs) mrbs = rbs;
    }                              
    return Max3(mls,mrs,mlbs+mrbs);
}
到目前为止,我已经分析了下面代码的操作

// T(n)
int M(int B[],int l,int r)
{
    if(l == r) return (B[l]>0)?B[l]:0;     // 1, because the comparation in the return
                                           //    will be rarely occur
    int m = (l+r)/2;                       // 3
    int mls = M(B,l,m);                    // T(n / 2) \
                                           //           -> 2T(n/2)
    int mrs = M(B,m+1,r);                  // T(n / 2) /
    int mlbs = 0, lbs = 0;                 // 2
    for(int i = m;i >= l;i--){             // loop -> n / 2
        lbs += B[i];                       //   1
        if(lbs > mlbs) mlbs = lbs;         //   1, if the element on the B is negative, then it
                                           //      produce a random pattern so we can ignore it
    }                                      // end loop
    int mrbs = 0, rbs =0;                  // 2
    for(int j = m+1;j <= r;j++){           // loop -> n / 2
        rbs += B[j];                       //   1
        if(rbs > mrbs) mrbs = rbs;         //   1, if the element on the B is negative, then it
                                           //      produce a random pattern so we can ignore it
    }                                      // end loop
    return Max3(mls,mrs,mlbs+mrbs);        // 3, Max3 would be O(2)
}
利用主定理,我们可以得到函数的时间复杂度为
nlogn

我的问题

如果我的操作不正确,我是否正确?如果可能,请解释问题?我怎样才能得到递归函数或数学表达式(我仍然搞不清两者的区别)

// T(n)
int M(int B[],int l,int r)
{
    if(l == r) return (B[l]>0)?B[l]:0;     // 1, because the comparation in the return
                                           //    will be rarely occur
    int m = (l+r)/2;                       // 3
    int mls = M(B,l,m);                    // T(n / 2) \
                                           //           -> 2T(n/2)
    int mrs = M(B,m+1,r);                  // T(n / 2) /
    int mlbs = 0, lbs = 0;                 // 2
    for(int i = m;i >= l;i--){             // loop -> n / 2
        lbs += B[i];                       //   1
        if(lbs > mlbs) mlbs = lbs;         //   1, if the element on the B is negative, then it
                                           //      produce a random pattern so we can ignore it
    }                                      // end loop
    int mrbs = 0, rbs =0;                  // 2
    for(int j = m+1;j <= r;j++){           // loop -> n / 2
        rbs += B[j];                       //   1
        if(rbs > mrbs) mrbs = rbs;         //   1, if the element on the B is negative, then it
                                           //      produce a random pattern so we can ignore it
    }                                      // end loop
    return Max3(mls,mrs,mlbs+mrbs);        // 3, Max3 would be O(2)
}
T(n) = 1 + 3 + T(n/2) + T(n/2) + 2 + 2 * (n/2) + 2 + (n/2) * (2 + 1) + 2 + 2 * (n/2) + 2 + (n/2) * (2 + 1) + 3
Then,
T(n) = 2 * T(n/2) + 3n + 15