Alle Herausforderungen
Schwierig 0 gelöst 0% Erfolgsquote

Matrix Chain Multiplication

Find the cheapest order to multiply a chain of matrices.

Problem

Matrix i has size dimensions[i] × dimensions[i+1]. Multiplying a p×q matrix by a q×r matrix costs p·q·r scalar multiplications. Choose where to put parentheses so multiplying the whole chain costs the least.

Implement min_multiplications(dimensions).

Input

dimensions: n + 1 sizes describing n matrices.

Output

Return the minimum total cost.

Einschränkungen

2 ≤ length of dimensions ≤ 200; 1 ≤ each size ≤ 500

Beispiele

Beispiel 1
Eingabe:
dimensions = [10, 30, 5, 60]

Erwartete Ausgabe:
expected = 4500
Eingabe:
std::vector<long long> dimensions = {10, 30, 5, 60};

Erwartete Ausgabe:
long long expected = 4500;
Eingabe:
long[] dimensions = new long[] {10, 30, 5, 60};

Erwartete Ausgabe:
long expected = 4500;

Erläuterung

(A×B)×C costs 10·30·5 + 10·5·60 = 4500, cheaper than A×(B×C) = 27000.

Beispiel 2
Eingabe:
dimensions = [5, 10]

Erwartete Ausgabe:
expected = 0
Eingabe:
std::vector<long long> dimensions = {5, 10};

Erwartete Ausgabe:
long long expected = 0;
Eingabe:
long[] dimensions = new long[] {5, 10};

Erwartete Ausgabe:
long expected = 0;

Erläuterung

A single matrix needs no multiplication: 0.

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.