Matrix Chain Multiplication
Given an integer array nums of N dimensions, there are N - 1 matrices in a fixed order. Matrix i has nums[i - 1] rows and nums[i] columns, for i from 1 to N -…
Given an integer array `nums` of N dimensions, there are N - 1 matrices in a fixed order. Matrix i has `nums[i - 1]` rows and `nums[i]` columns, for i from 1 to N - 1. Return the minimum number of scalar multiplications needed to multiply the chain. You may choose the parenthesization, but you may not reorder the matrices. Multiplying an a × b matrix by a b × c matrix costs a × b × c scalar multiplications and produces an a × c matrix. A chain containing one matrix needs zero multiplications.
- `2 <= nums.length <= 100` - `1 <= nums[i] <= 100` - Return one integer cost, not a parenthesization or the product matrix. - The visualizer accepts at most 6 dimensions (5 matrices); longer valid inputs are execution-only.
Example 1: input nums = [10, 15, 20, 25]; output 8000. (A1 × A2) × A3 costs 10 × 15 × 20 + 10 × 20 × 25 = 8000. A1 × (A2 × A3) costs 11250.
Example 2: input nums = [4, 2, 3]; output 24. Two matrices have only one multiplication, costing 4 × 2 × 3 = 24.
Example 3: input nums = [5, 10]; output 0. Two dimensions describe one matrix, so there is nothing to multiply.
Category: Arrays. Topic: dynamic-programming. Difficulty: hard. Patterns: dynamic-programming.