Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 10 Dec, 2020

Moderate

```
For arr[ ] = { 10, 20, 30, 40}, matrix A1 = [10 * 20], A2 = [20 * 30], A3 = [30 * 40]
Scalar multiplication of matrix with dimension 10 * 20 is equal to 200.
```

```
The first line of input contains an integer ‘T’, denoting the number of test cases. Then each test case follows.
The first line of each test case contains the Integer ‘N’ denoting the number of elements in the array.
The second and the last line of each test case contains ‘N’ single space-separated integers representing the elements of the array.
```

```
For each test case, print a single integer, denoting the minimum cost of matrix multiplication.
Output of each test case will be printed on a separate line.
```

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

```
1 <= T <= 5
2 <= N <= 100
1 <= arr[i] <= 400
Time Limit: 1 sec.
```

The idea is to try out all possible combinations of parentheses from left to right and avoid the computation of repetitive subproblems with memoization.

- place parentheses at all possible places,
- calculate the cost for each placement and return the minimum value.
- In a chain of matrices of size ‘ n’, we can place the first set of parentheses in ‘n’ - 1 way.

Minimum number of multiplication needed to multiply a chain of size n = Minimum of all ‘n ‘-1 placements (these placements create subproblems of smaller size)

Therefore, the problem has optimal substructure property and can be easily solved using recursion.

Also, there is a lot of repetition in subproblems hence do memoization.

Take ‘dp[102][102]’ a global 2D matrix, for memoization.

Let matrixMultiplication(arr, N) be the function, returns the minimum cost of matrix multiplication.

- Initialize ‘dp’ with -1.
- Make a helper function ‘calculateCost’ that takes three parameters (arr, 1, N - 1).
- Call the ‘calculateCost’ function and store the return answer in the ‘ans’ variable.
- Return ‘ans’

It takes three parameters (arr, i, j) and returns the minimum cost of matrix multiplication of matrices that end ‘i’ to ‘j’ index in the array.

- If ’i’ is greater than or equal to ‘j’
- Return 0

- If ‘dp[i][j] is not equal to -1
- Return ‘dp[i][j]’

- Take a variable ‘ans’ and initialize it with infinity (INT_MAX)
- Run a loop from ‘i’ to ‘j’ - 1
- Take a variable ‘temp’ and store the temporary answer i.e calculateCost(arr, i, k) + calculateCost(arr, k + 1, j) + (arr[k] * arr[i - 1] * arr[j])
- If temporary answer ‘temp’ is smaller than ‘ans’, then update ‘ans’ with ‘temp’

- Store ‘ans’ at ‘dp[i][j]’
- Return ‘ans’

The idea is to solve the smaller problem first.

Take 2D matrix dp[N][N]

dp[i][j] = Minimum number of scalar multiplications needed to compute the matrix A[i]A[i + 1]...A[j] = A[i..j] where dimension of A[i] is arr[i - 1] x arr[i]

The cost of matrix multiplication of a single matrix is 0.

Then, solve for length 2 to N - 1. ( there are N - 1 matrix in N length array) using the following transition.

Dp[i][j] = min( dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j]) where i <= k < j.

The answer is stored in dp[1][N - 1]

- Take a 2D array of size N * N ‘dp[N][N]’
- Run a loop from 1 to N - 1
- Initialize dp[i][i] equal to 0.

- Take a counter say ‘l’, and run a loop from 2 to N - 1
- Take a counter ‘i’ and run a loop from 1 to N - l
- Take a variable ‘j’ = ‘i’ + ‘l’ -1
- Initialize dp[i][j] to infinity.
- Take a counter ‘k’ and run a loop from ‘i’ to ‘j’ - 1.
- Take a variable ‘temp’ and store the temporary answer i.e dp[i][k] + dp[k+1][j] + arr[i - 1] * arr[k] * arr[j].
- If the temporary answer 'temp' is less than the actual answer then update the actual answer i.e dp[i][j] = ‘temp’.

- Take a counter ‘i’ and run a loop from 1 to N - l
- Return dp[1][N - 1].