-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSolution343_2.java
More file actions
49 lines (36 loc) · 881 Bytes
/
Solution343_2.java
File metadata and controls
49 lines (36 loc) · 881 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package dynamic_problem;
import array.Array;
import java.util.Arrays;
/**
* 记忆化搜索 + 暴力搜索
* O(n ^ 2)
* O(n)
*/
public class Solution343_2 {
private int[] memo;
public int integerBreak(int n) {
if (n < 1) {
throw new IllegalArgumentException("Illegal argument!");
}
memo = new int[n + 1];
Arrays.fill(memo, -1);
return breakInteger(n);
}
private int breakInteger(int n) {
if (n == 1) {
return 1;
}
if (memo[n] != -1) {
return memo[n];
}
int res = -1;
for (int i = 1; i <= n - 1; i++) {
res = max3(res, i * (n - i), i * breakInteger(n - i));
}
memo[n] = res;
return res;
}
private int max3(int a, int b, int c) {
return Math.max(a, Math.max(b, c));
}
}