- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem312.java
More file actions
Latest commit
39 lines (30 loc) · 1023 Bytes
/
Copy pathProblem312.java
File metadata and controls
39 lines (30 loc) · 1023 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
importjava.util.List;
importjava.util.ArrayList;
classProblem312 {
publicstaticvoidmain(String[] args) {
int[] nums = {3,1,5,8};
System.out.println(newProblem312().maxCoins(nums));
}
publicintmaxCoins(int[] nums) {
List<Integer> list = newArrayList<>();
list.add(1);
for (inti = 0; i < nums.length; i++) {
list.add(nums[i]);
}
list.add(1);
int[][] dp = newint[list.size()][list.size()];
intsum = helper(list, 1, nums.length, dp);
returnsum;
}
inthelper(List<Integer> list, intleft, intright, int[][] dp) {
if (left > right) return0;
if (dp[left][right] > 0) returndp[left][right];
for (inti = left; i <= right; i++) {
intl = helper(list, left, i-1, dp);
intr = helper(list, i+1, right, dp);
intval = list.get(i) * list.get(left-1) * list.get(right+1);
dp[left][right] = Math.max(dp[left][right], val + l + r);
}
returndp[left][right];
}
}