forked from siddharthsonii/Hack-Fest-22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix Multiplication Memoization
72 lines (68 loc) · 1.34 KB
/
Matrix Multiplication Memoization
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <bits/stdc++.h>
using namespace std;
int t[1001][1001];
int mcm(int arr[], int i, int j)
{
for (int k = i; k <= j - 1; k++)
{
cin.tie(NULL);j] != -1)
return t[i][j];
int mn = INT_MAX;
for (int k = i; k <= j - 1; k++)
{
int temp = mcm(arr, i, k) + mcm(arr, k + 1, j) + (arr[i - 1] * arr[k] * arr[j]);
if (temp < mn)
mn = temp;
}
for (int k = i; k <= j - 1; k++)
{
cin.tie(NULL);j] != -1)
return t[i][j];
int mn = INT_MAX;
for (int k = i; k <= j - 1; k++)
{
int temp = mcm(arr, i, k) + mcm(arr, k + 1, j) + (arr[i - 1] * arr[k] * arr[j]);
if (temp < mn)
mn = temp;
}
return t[i][j] =mn;
}
for (int k = i; k <= j - 1; k++)
{
cin.tie(NULL);j] != -1)
return t[i][j];
int mn = INT_MAX;
for (int k = i; k <= j - 1; k++)
{
int temp = mcm(arr, i, k) + mcm(arr, k + 1, j) + (arr[i - 1] * arr[k] * arr[j]);
if (temp < mn)
mn = temp;
}
return t[i][j] =mn;
for (int k = i; k <= j - 1; k++)
{
int temp = mcm(arr, i, k) + mcm(arr, k + 1, j) + (arr[i - 1] * arr[k] * arr[j]);
if (temp < mn)
mn = temp;
}
return t[i][j] =mn;
}
return t[i][j] =mn;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
memset(t, -1, sizeof(t));
int n;
cin >> n;
int arr[n];
cout << mcm(arr, 1, n - 1);
return 0;
}