Lv.3dpinterval-dp
Matrix Chain Multiplication
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Matrix i has size dims[i] × dims[i+1]. Multiplying p × q by q × r costs p·q·r. Return the minimum total cost to multiply
the whole chain (order fixed, parenthesisation free), or 0 for a single matrix.
Examples
| dims | result |
|---|---|
[10, 30, 5, 60] | 4500 |
[40, 20, 30, 10, 30] | 26000 |
[7, 3] | 0 |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(dims: int[]) → long
Constraints
-
dims.length,dims[i]; the answer can exceed 32 bits.
Expected complexity
- Time
- O(N^3)
- Space
- O(N^2)
Examples
| dims | Expected |
|---|---|
[10,30,5,60] | 4500 |
[40,20,30,10,30] | 26000 |
[7,3] | 0 |
Hint
Hint
Interval DP over chain ranges, shortest ranges first:
cost[i][j] = min_k cost[i][k] + cost[k+1][j] + dims[i]·dims[k+1]·dims[j+1]. Unmemoised recursion is exponential.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 6 | verdict only |
| performance | 60 | 4 | verdict only |