first thought
- Build a 2d array, represent the minimum from i to j. i and j are the split index of the input.
- Distance between i and j must be larger than 1.
If i == j, the value is 0.(base case)dp[i][j] = dp[i] * dp[i-1][j-1] * dp[j].
- dp[i][j] = Math.min(dp[i][j-1] + input[i] * input[j-1] * input[j], dp[i+1][j] + input[i] * input[i+1] * input[j])
- Base case is k == 2, which means the minimum distance of i and j, only two Matrix Multiplication.
- Increase the distance from 3 to n.
solution
|
|
result
Passed the three test case in gfg, but the code is not the same. I’m not sure whether mine is a correct one.