forked from liuyubobobo/Play-with-Algorithm-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution3.java
More file actions
32 lines (25 loc) · 868 Bytes
/
Solution3.java
File metadata and controls
32 lines (25 loc) · 868 Bytes
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
/// 343. Integer Break
/// https://leetcode.com/problems/integer-break/description/
/// 动态规划
/// 时间复杂度: O(n^2)
/// 空间复杂度: O(n)
public class Solution3 {
public int integerBreak(int n) {
if(n < 1)
throw new IllegalArgumentException("n should be greater than zero");
int[] memo = new int[n+1];
memo[1] = 1;
for(int i = 2 ; i <= n ; i ++)
// 求解memo[i]
for(int j = 1 ; j <= i - 1 ; j ++)
memo[i] = max3(memo[i], j * (i - j), j * memo[i - j]);
return memo[n];
}
private int max3(int a, int b, int c){
return Math.max(a, Math.max(b, c));
}
public static void main(String[] args) {
System.out.println((new Solution3()).integerBreak(2));
System.out.println((new Solution3()).integerBreak(10));
}
}