Optimize Matrix-Chain Multiplication
Company: Gsa
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Implement `minimum_matrix_chain_cost(dimensions)`.
There are `n = len(dimensions) - 1` matrices. Matrix `A_i` has shape `dimensions[i-1] x dimensions[i]` for one-based `i`. Choose a parenthesization that minimizes the number of scalar multiplications and return that minimum cost. Matrix order cannot be changed.
### Constraints
- `2 <= len(dimensions) <= 501`
- `1 <= dimensions[i] <= 10000`
- Return a 64-bit integer.
### Examples
- `[10, 30, 5, 60]` returns `4500`, using `(A1 A2) A3`.
- `[40, 20, 30, 10, 30]` returns `26000`.
- Two dimensions describe one matrix and return cost `0`.
```hint The final multiplication splits an interval
For matrices `i..j`, try each final split `k`; the subchains are independent once the split is fixed.
```
```hint Fill by increasing chain length
Every transition depends only on shorter intervals.
```
Quick Answer: Implement `minimum_matrix_chain_cost(dimensions)`. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.
For dimensions of `n` compatible matrices, choose a parenthesization minimizing the number of scalar multiplications without changing matrix order. Matrix `A_i` has shape `dimensions[i-1]` by `dimensions[i]`. Return the exact minimum 64-bit cost.
Constraints
- 2 <= len(dimensions) <= 501, describing one through 500 matrices.
- Every dimension is an integer from 1 through 10000.
- Matrix order cannot change; return the minimum scalar multiplication count as an exact 64-bit integer.
- Two dimensions describe one matrix and therefore cost zero.
Examples
Input: ([10, 20],)
Expected Output: 0
Explanation: One matrix requires no multiplication.
Input: ([10, 20, 30],)
Expected Output: 6000
Explanation: Two matrices have exactly one multiplication order.
Hints
- Test one matrix, two matrices, and chains with several possible parenthesizations.
- Include thin dimensions equal to one and maximum dimensions equal to ten thousand.
- Use increasing, decreasing, and repeated dimension patterns.