Problem · Array

Minimum Matrix-Chain Multiplications

Learn this problem
HardGSA Capital logoGSA CapitalFULLTIMEPHONE SCREEN

Problem 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[]) → long

Examples

Example 1

dimensions = [40,20,30,10,30]return = 26000

The best order is ((A1 x (A2 x A3)) x A4), with cost 6000 + 8000 + 12000 = 26000.

Example 2

dimensions = [10,20,30]return = 6000

Two matrices have only one possible multiplication order, costing 10 x 20 x 30 = 6000.

Example 3

dimensions = [10,30,5,60]return = 4500

Multiplying 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.
drafts saved locally
public long minimumMultiplications(int[] dimensions) {
    // write your code here
}
dimensions[40,20,30,10,30]
expected26000
checking account