topic
You give a string of length n, please cut the rope length of the integer m sections (m, n are integers, n> 1 and m> 1), the length of each rope is referred to as k [0], k [1] ... k [m] . Will k [0] k [1] ... * k [m] maximum possible product is how much? For example, when the length of the rope is 8:00, we cut it into lengths of three sections 2,3,3 of the product obtained at this time is the maximum 18.
Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/jian-sheng-zi-lcof
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Thinking
I thought it was an ordinary dp
overlooked an important point, that is, those kinds of dp [ij] there are two states, can no longer split and split
reward
Special DP, section title to remember that in the equation and then provided a dp max ()
Code
class Solution {
//dp[n]:长度为n的绳子的最大乘积
//dp[1]=1.dp[2]=1
public int[] dp;
public int cuttingRope(int n) {
dp=new int[n+1];
dp[1] =1;dp[2]=1;
for(int i= 2;i<n+1;i++){
for(int j=1;j<i+1&&(i-j)>0;j++){
dp[i] =Math.max(dp[i],Math.max((i-j)*j,j*dp[i-j]));
}
}
return dp[n];
}
}