Sword Finger Offer Interview Question 14- I. Cutting the Rope [Medium]-Dynamic Programming

Problem solving:

Use dp [i] to store the maximum product of the rope at length i, which is related to the previous maximum product of 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];
    }
};

Published 65 original articles · Like1 · Visits 484

Guess you like

Origin blog.csdn.net/qq_41041762/article/details/105472442