剑指offer 面试题14- I. 剪绳子 [中等]——动态规划

解题:

用dp[i]保存长度为i时的绳子的最大乘积,它与之前所有0~i-1的最大乘积有关

class Solution {
public:
    //vector<int> arr;
    int cuttingRope(int n) {
        vector<int> dp(n+1,0);
        dp[1]=1;
        for(int i=2;i<=n;i++){
            for(int j=i-1;j>=1;j--)
                dp[i]=max(dp[i],max(j,dp[j])*(i-j));
        }
        return dp[n];
    }
};

发布了65 篇原创文章 · 获赞 1 · 访问量 484

猜你喜欢

转载自blog.csdn.net/qq_41041762/article/details/105472442