कठिन 0 हल किए गए 0% स्वीकृति

Matrix Chain Multiplication

Find the cheapest order to multiply a chain of matrices.

आप क्या सीखेंगे

  • Define DP over subchains
  • Try every split point
  • Minimize multiplication cost

समस्या

अनुवाद दिखाएँ

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.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

The order of multiplication changes the total cost even though the final matrix shape stays the same.

संकेत 2

Try every possible split point for each interval and reuse smaller interval results.

संकेत 3

Use the dimensions array carefully: each subproblem depends on the endpoints around the matrices, not the matrix count alone.

अपेक्षित जटिलता

O(n^3) time, O(n^2) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।