-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmcm_recursive.java
More file actions
30 lines (25 loc) · 810 Bytes
/
mcm_recursive.java
File metadata and controls
30 lines (25 loc) · 810 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
// step 1 : find i and j
// step 2 : find base condition
// step 3 : find k loop scheme
// step 4 : calculate temp ans then apply the function to get final ans
// great article to refer :
// time complexity for this naive solution is exponential
// * https://leetcode.com/discuss/general-discussion/1278305/all-about-matrix-chain-multiplication-easy *
public class mcm_recursive {
int solve(int i, int j,int arr[])
{
if(i>=j)return 0;
int ans=Integer.MAX_VALUE;
for(int k=i;k<=j-1;k++)
{
int tempAns = solve(i,k,arr) + solve(k+1,j,arr)
+ arr[i-1]*arr[k]*arr[j];
ans=Math.min(ans,tempAns);
}
return ans;
}
int matrixMultiplication(int N, int arr[])
{
return solve(1,N-1,arr);
}
}