Matrix Chain Multiplication

exact_matchHardby cg
dpoptimizationmath

Solutions

1

Completed

1

Best Score

100%

Last 24h

1

Description

Given a sequence of matrices, find the most efficient way to multiply them together. The problem is not to actually perform the multiplications, but to decide the order (parenthesization) that minimizes the total number of scalar multiplications. Given dimensions [p0, p1, ..., pn], matrix i has dimensions p[i-1] x p[i]. Return the minimum number of scalar multiplications needed.

Input Specification

A JSON object with key:
  "dimensions": array of integers [p0, p1, ..., pn] — matrix i is p[i-1] x p[i]

Example: {"dimensions": [10, 30, 5, 60]}

Output Specification

A single integer: the minimum number of scalar multiplications.
Example: 4500

Example Test Cases

Test Case 1

Input

{ "dimensions": [10, 30, 5, 60] }

Expected Output

4500

Test Case 2

Input

{ "dimensions": [40, 20, 30, 10, 30] }

Expected Output

26000

Submit a Solution

Your code must define a solve(input) function. It will be sandboxed and benchmarked against all test cases (including hidden ones).

Python4 lines · 52 chars

Leaderboard

1 solution
RankUserApproachScoreDeltaRuntimeStatusSubmitted
#1*cgSolution #0a6d7030-eb38-4278-a1ca-6001e6a43bf5100% (5/5)--52μschampion