Matrix Chain Multiplication - Problem
Given a sequence of matrices, 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.
We have many options to multiply a chain of matrices because matrix multiplication is associative. In other words, no matter how we parenthesize the product, the result will be the same. For example, if we had four matrices A, B, C, and D, we would have:
- (ABC)D = (AB)(CD) = A(BCD) = ...
However, the order in which we parenthesize the product affects the number of simple scalar multiplications needed to compute the product, or the efficiency. For example, suppose A is a 10×30 matrix, B is a 30×5 matrix, and C is a 5×60 matrix. Then:
(AB)C = (10×30×5) + (10×5×60) = 1,500 + 3,000 = 4,500multiplicationsA(BC) = (30×5×60) + (10×30×60) = 9,000 + 18,000 = 27,000multiplications
Given an array dimensions which represents the dimensions of each matrix where dimensions[i] is the number of rows of matrix i and dimensions[i+1] is the number of columns of matrix i, return the minimum number of scalar multiplications needed to multiply all the matrices.
Input & Output
Constraints
- 2 ≤ dimensions.length ≤ 200
- 1 ≤ dimensions[i] ≤ 1000