Leetcode 375. Guess the size of the number II (DAY 45) ---- Dynamic programming learning period (AC feeling at that moment is back!)

Original title

Insert picture description here



Code implementation (the first brush is basically equal to self-solving)

int getMoneyAmount(int n){
    
    
    int dp[n+1][n+1],end,start,dvalue,pos;
    memset(dp,0,sizeof(dp));
    for(end=2;end<=n;end++)
    {
    
    
        for(start = end-1;start>=1;start--)
        {
    
    
            dvalue = end - start;
            if(dvalue == 1) dp[start][end] = start;
            else
            {
    
    
                dp[start][end] = INT_MAX;
                for(pos = end-1; pos > start; pos--)
                    dp[start][end] = fmin(dp[start][end],fmax(pos+dp[pos+1][end],pos+dp[start][pos-1]));
            }
        }
    }
    return dp[1][n];
}

Guess you like

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