Problem · Array
Minimum Matrix-Chain Multiplications
Learn this problemProblem statement
An ordered chain contains n compatible matrices. Matrix i has dimensions dimensions[i - 1] x dimensions[i]. Matrix multiplication is associative, so the final product is unchanged by parenthesization, but the number of scalar multiplications may differ.
Return the minimum scalar-multiplication count needed to multiply the entire chain. Do not materialize the matrices.
Function
minimumMultiplications(dimensions: int[]) → longExamples
Example 1
dimensions = [40,20,30,10,30]return = 26000The best order is ((A1 x (A2 x A3)) x A4), with cost 6000 + 8000 + 12000 = 26000.
Example 2
dimensions = [10,20,30]return = 6000Two matrices have only one possible multiplication order, costing 10 x 20 x 30 = 6000.
Example 3
dimensions = [10,30,5,60]return = 4500Multiplying the first two matrices before the third costs 1500 + 3000 = 4500.
Constraints
2 <= dimensions.length <= 101- Every dimension is a positive integer.
- Every scalar product and every intermediate candidate sum evaluated by the recurrence is less than
2^62.