forked from keshavagarwal17/All-Cp-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_chain_multiplication.java
54 lines (45 loc) · 1.37 KB
/
matrix_chain_multiplication.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Initial Template for Java
import java.io.*;
import java.util.*;
class matrix_chain
{
public static void main(String args[])throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
while(t-- > 0){
int N = Integer.parseInt(in.readLine());
String input_line[] = in.readLine().trim().split("\\s+");
int arr[]= new int[N];
for(int i = 0; i < N; i++)
arr[i] = Integer.parseInt(input_line[i]);
Solution ob = new Solution();
System.out.println(ob.matrixMultiplication(N, arr));
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution{
static int[][] memo = new int[100][100];;
static int matrixMultiplication(int n, int arr[])
{
for(int[] row:memo)
Arrays.fill(row,-1);
return solve(arr,1,n-1);
}
public static int solve(int [] a,int i,int j)
{
if(i>=j)
return 0;
if(memo[i][j]!=-1)
return memo[i][j];
memo[i][j] = Integer.MAX_VALUE;
for(int k = i;k<j;k++)
{
int x = solve(a,i,k)+solve(a,k+1,j)+(a[i-1]*a[k]*a[j]);
memo[i][j]=Math.min(memo[i][j],x);
}
return memo[i][j];
}
}