-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCutRopeLCOFII.java
More file actions
62 lines (36 loc) · 1.31 KB
/
CutRopeLCOFII.java
File metadata and controls
62 lines (36 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package recursion_and_dynamic_programming;
import java.math.BigInteger;
import java.util.Arrays;
/**
* @Author: Wenhang Chen
* @Description:给你一根长度为 n 的绳子,请把绳子剪成整数长度的 m 段(m、n都是整数,n>1并且m>1),每段绳子的长度记为 k[0],k[1]...k[m] 。请问 k[0]*k[1]*...*k[m] 可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。
* <p>
* 答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
* <p>
*
* <p>
* 示例 1:
* <p>
* 输入: 2
* 输出: 1
* 解释: 2 = 1 + 1, 1 × 1 = 1
* 示例 2:
* <p>
* 输入: 10
* 输出: 36
* 解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36
* @Date: Created in 9:22 4/19/2020
* @Modified by:
*/
public class CutRopeLCOFII {
public int cuttingRope(int n) {
BigInteger dp[] = new BigInteger[n + 1];
Arrays.fill(dp, BigInteger.valueOf(1));
for (int i = 3; i <= n; i++) {
for (int j = 1; j < i; j++) {
dp[i] = dp[i].max(BigInteger.valueOf(j * (i - j))).max(dp[i - j].multiply(BigInteger.valueOf(j)));
}
}
return dp[n].mod(BigInteger.valueOf(1000000007)).intValue();
}
}