-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDp343.java
More file actions
43 lines (41 loc) · 1.2 KB
/
Copy pathDp343.java
File metadata and controls
43 lines (41 loc) · 1.2 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
40
41
42
43
package dynamicprogramming;
import java.util.Arrays;
/**
* @ProjectName: leetcode
* @Package: dynamicprogramming
* @ClassName: Dp343
* @Author: markey
* @Description:343. 整数拆分
* 给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。
*
* 示例 1:
*
* 输入: 2
* 输出: 1
* 解释: 2 = 1 + 1, 1 × 1 = 1。
* 示例 2:
*
* 输入: 10
* 输出: 36
* 解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36。
* 说明: 你可以假设 n 不小于 2 且不大于 58。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/integer-break
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2020/4/13 22:47
* @Version: 1.0
*/
public class Dp343 {
public int integerBreak(int n) {
int[] dp = new int[n+1];
dp[0] = dp[1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 2; j <= i / 2; j++) {
dp[i] = Math.max(dp[i], Math.max(j, dp[j]) * Math.max(i-j, dp[i-j]));
}
}
System.out.println(Arrays.toString(dp));
return dp[n];
}
}