Matrix Chain Multiplication

Given an array of integers A representing chain of 2-D matices such that the dimensions of ith matrix is
A[i-1] x A[i].

Find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications,
but merely to decide in which order to perform the multiplications.

Return the minimum number of multiplications needed to multiply the chain.



Input Format

The only argument given is the integer array A.

Output Format

Return the minimum number of multiplications needed to multiply the chain.

Constraints

1 <= length of the array <= 1000
1 <= A[i] <= 100

For Example

Input 1:
    A = [40, 20, 30, 10, 30]
Output 1:
    26000
    Explanation 1:
        Dimensions of A1 = 40 x 20
        Dimensions of A2 = 20 x 30
        Dimensions of A3 = 30 x 10
        Dimensions of A4 = 10 x 30
        First, multiply A2 and A3 ,cost = 20*30*10 = 6000
        Second, multilpy A1 and (Matrix obtained after multilying A2 and A3) =  40 * 20 * 10 = 8000
        Third, multiply (Matrix obtained after multiplying A1, A2 and A3) and A4 =  40 * 10 * 30 = 12000
        Total Cost = 12000 + 8000 + 6000 =26000

Input 2:
    A = [10, 20, 30] 
Output 2:
    6000
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Matrix Chain Multiplication on Interview Code Editor
Sign Up
to access hints and editorial solutions for Matrix Chain Multiplication

Discussion


Loading...
Click here to start solving coding interview questions