JZ67 Cut the rope

Title description

Here is a rope with a length of n. Please cut the rope into m segments of integer length (m and n are integers, n>1 and m>1, m<=n), and the length of each segment is recorded as k [1],...,k[m]. What is the maximum possible product of k[1]x...xk[m]? For example, when the length of the rope is 8, we cut it into three pieces with lengths of 2, 3, and 3. The maximum product obtained at this time is 18.

Enter description:

Enter a number n, see the title for the meaning. (2 <= n <= 60)

Output description:

Output the answer.
Example 1

enter

8

Output

18

public class Solution {
        public int cutRope(int target) {
        if (target == 2) {
            return 1;
        }
        if (target == 3) {
            return 2;
        }
        int[] dp = new int[target+1];
        dp[1] = 1;
        dp[2] = 2;
        dp[3] = 3;
        for (int i = 1; i <= target; i++) {
            for (int j = 1; j <= i/2; j++) {
                dp[i] = Math.max(dp[i],dp[j]*dp[i-j] );
            }
        }
        return dp[target];
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41620020/article/details/108705391