-
Notifications
You must be signed in to change notification settings - Fork 0
/
brackets.java
38 lines (34 loc) · 1.21 KB
/
brackets.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
34
35
36
37
38
class Solution{
static String matrixChainOrder(int p[], int n){
// code here
int[][] m = new int[n][n];
int[][] s = new int[n][n];
// m[i][i] is zero as the cost of multiplying one matrix is zero
for (int i = 1; i < n; i++) {
m[i][i] = 0;
}
// L is chain length
for (int L = 2; L < n; L++) {
for (int i = 1; i < n - L + 1; i++) {
int j = i + L - 1;
m[i][j] = Integer.MAX_VALUE;
for (int k = i; k < j; k++) {
int cost = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (cost < m[i][j]) {
m[i][j] = cost;
s[i][j] = k;
}
}
}
}
// Constructing the output string
return printOptimalParens(s, 1, n - 1);
}
static String printOptimalParens(int[][] s, int i, int j) {
if (i == j) {
return String.valueOf((char)('A' + i - 1)); // Naming matrices as 'A', 'B', 'C', etc.
} else {
return "(" + printOptimalParens(s, i, s[i][j]) + printOptimalParens(s, s[i][j] + 1, j) + ")";
}
}
}