1189. Matrix Chain Multiplication

MediumDynamic ProgrammingInterval DP

Given dimensions dims where matrix i has size dims[i-1] x dims[i], find the minimum number of scalar multiplications needed to multiply the whole chain of matrices (the multiplication order is free). The input is JSON {dims}.

Input: JSON {dims}.

Output: Integer — the minimum scalar multiplications.

Examples

Example 1
Input: {"dims":[1,2,3,4]}
Output: 18
Explanation: Optimal parenthesization costs 18.
Example 2
Input: {"dims":[40,20,30,10,30]}
Output: 26000
Explanation: Optimal order costs 26000.
Example 3
Input: {"dims":[10,20,30]}
Output: 6000
Explanation: Single multiplication.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →