0 |
---|
0 |
---|
function matrixChainOrder(p) {let n = p.length - 1;let m = Array(n + 1).fill().map(() => Array(n + 1).fill(Infinity));let s = Array(n + 1).fill().map(() => Array(n + 1).fill(0));for (let i = 1; i <= n; i++) {m[i][i] = 0;}for (let L = 2; L <= n; L++) {for (let i = 1; i <= n - L + 1; i++) {let j = i + L - 1;for (let k = i; k < j; k++) {let q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];if (q < m[i][j]) {m[i][j] = q;s[i][j] = k;}}}}return { m, s };}