Leetcode's sword refers to Offer 14- II. Cut the rope II (DAY 53) ----Dynamic programming learning period (involving large number operations, unfortunately C language is not available)

Original title

Insert picture description here



Code implementation (first brush self-solving but beyond the range of large numbers)

int cuttingRope(int n){
    
    
    long dp[1001] = {
    
    0};
    int i,j,temp;
    for(i=1;i<=n;i++)
    {
    
    
        if(i != n)  dp[i] = i;
        temp = i>>1;
        for(j=1;j<=temp;j++)
            dp[i] = fmax(dp[i],dp[j]*dp[i-j]);
    }
    return dp[n] % 1000000007;
}


Code implementation (greedy algorithm is indeed a mathematical solution)

int cuttingRope(int n){
    
    
    long ret = 1;
    int i;
    if(n==2)    return 1;
    if(n==3)    return 2;
    while(n>4)
    {
    
    
        ret *= 3;
        ret %= 1000000007;
        n-=3;
    }
    return (int)(ret*n%1000000007);
}

Guess you like

Origin blog.csdn.net/qq_37500516/article/details/114076242