LeetCode力扣剪繩子

動態規劃:

class Solution {
public:
    
    int cuttingRope(int n) {
        if(n==2)
            return 1;
        else if(n==3)
            return 2;
        else{
            int dp[60];
            dp[1]=1;
            dp[2]=2;
            dp[3]=3;
            for(int i=4;i<=n;i++)
            {
                int max=0;
                for(int j=1;j<=i/2;j++)
                {
                    int tmp=dp[j]*dp[i-j];
                    if(tmp>max)
                        max=tmp;
                }
                dp[i]=max;
            }
            return dp[n];
        }
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章