-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay43.java
More file actions
32 lines (27 loc) · 931 Bytes
/
Day43.java
File metadata and controls
32 lines (27 loc) · 931 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
public class Day43 {
public static int matrixChainOrder(int[] arr) {
int n = arr.length - 1;
int[][] dp = new int[n][n];
for (int i = 1; i < n; i++) {
dp[i][i] = 0;
}
for (int len = 2; len < n; len++) {
for (int i = 1; i < n - len + 1; i++) {
int j = i + len - 1;
dp[i][j] = Integer.MAX_VALUE;
for (int k = i; k < j; k++) {
int cost = dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j];
if (cost < dp[i][j]) {
dp[i][j] = cost;
}
}
}
}
return dp[1][n - 1];
}
public static void main(String[] args) {
int[] arr = {40, 20, 30, 10, 30};
int result = matrixChainOrder(arr);
System.out.println("Minimum number of multiplications: " + result);
}
}