-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_1130.java
More file actions
46 lines (42 loc) · 1.42 KB
/
Copy pathP_1130.java
File metadata and controls
46 lines (42 loc) · 1.42 KB
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
package leetcode.medium;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
public class P_1130 {
public int mctFromLeafValues(int[] arr) {
final int n = arr.length;
final int[][] dp = new int[n][n];
final int[][] max = new int[n][n];
for (int i = 0; i < arr.length; i++) {
int localMax = 0;
for (int j = i; j < arr.length; j++) {
localMax = Math.max(localMax, arr[j]);
max[i][j] = localMax;
}
}
for (int l = 1; l < n; ++l) {
for (int i = 0; i < n - l; ++i) {
final int j = i + l;
dp[i][j] = Integer.MAX_VALUE;
for (int k = i; k < j; ++k) {
dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k + 1][j] + max[i][k] * max[k + 1][j]);
}
}
}
return dp[0][n - 1];
}
public int mctFromLeafValuesGreedy(int[] arr) {
int res = 0;
final Deque<Integer> stack = new ArrayDeque<>(Collections.singletonList(Integer.MAX_VALUE));
for (int num : arr) {
while (stack.getFirst() <= num) {
res += stack.removeFirst() * Math.min(stack.getFirst(), num);
}
stack.push(num);
}
while (stack.size() > 2) {
res += stack.removeFirst() * stack.getFirst();
}
return res;
}
}