-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxProductPlus.java
33 lines (30 loc) · 936 Bytes
/
MaxProductPlus.java
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
/**
* Created by Yuzhang on 1/26/17.
*/
public class MaxProductPlus {
private static int maxProductPlus(int[] array) {
if (array == null || array.length == 0) {
return 0;
}
int n = array.length;
int[][] dp = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = i; j >= 0; j--) {
if (i == j) {
dp[j][i] = array[i];
} else {
dp[j][i] = Integer.MIN_VALUE;
for (int k = j; k < i; k++) {
dp[j][i] = Math.max(dp[j][i], Math.max((dp[j][k] + dp[k + 1][i]), (dp[j][k] * dp[k + 1][i])));
}
}
}
}
return dp[0][n - 1];
}
public static void main (String[] args) {
int[] array = {1,2,3};
int result = maxProductPlus(array);
System.out.println(result);
}
}