Integer Break LeetCode solution in Java

1
class Solution {
2
public int integerBreak(int n) {
3
if (n <= 1) {
4
return 0;
5
}
6
int[] memo = new int[n + 1];
7
return maxProduct(n, memo);
8
}
9

10
private int maxProduct(int n, int[] memo) {
11
if (n == 2) {
12
return 1;
13
}
14
if (memo[n] != 0) {
15
return memo[n];
16
}
17
int max = 0;
18
for (int i = 1; i < n; i++) {
19
max = Math.max(max, Math.max(i * (n - i), i * maxProduct(n - i, memo)));
20
}
21
memo[n] = max;
22
return max;
23
}
24
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0