ยาก 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Matrix Chain Multiplication

Find the cheapest order to multiply a chain of matrices.

โจทย์

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.

เงื่อนไข

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

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
dimensions = [10, 30, 5, 60]

ผลลัพธ์ที่คาดหวัง:
expected = 4500
อินพุต:
std::vector<long long> dimensions = {10, 30, 5, 60};

ผลลัพธ์ที่คาดหวัง:
long long expected = 4500;
อินพุต:
long[] dimensions = new long[] {10, 30, 5, 60};

ผลลัพธ์ที่คาดหวัง:
long expected = 4500;

คำอธิบาย

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

ตัวอย่างที่ 2
อินพุต:
dimensions = [5, 10]

ผลลัพธ์ที่คาดหวัง:
expected = 0
อินพุต:
std::vector<long long> dimensions = {5, 10};

ผลลัพธ์ที่คาดหวัง:
long long expected = 0;
อินพุต:
long[] dimensions = new long[] {5, 10};

ผลลัพธ์ที่คาดหวัง:
long expected = 0;

คำอธิบาย

A single matrix needs no multiplication: 0.

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก