From 2483c1c91428608bca1b8c10709858df28acc898 Mon Sep 17 00:00:00 2001 From: wang-peijun <42425151+wang-peijun@users.noreply.github.com> Date: Sat, 5 Jun 2021 11:49:41 +0800 Subject: [PATCH 1/2] fix knapsack.js findValues --- src/js/algorithms/dynamic-programing/knapsack.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/js/algorithms/dynamic-programing/knapsack.js b/src/js/algorithms/dynamic-programing/knapsack.js index 9c979360..d72e819d 100644 --- a/src/js/algorithms/dynamic-programing/knapsack.js +++ b/src/js/algorithms/dynamic-programing/knapsack.js @@ -1,4 +1,4 @@ -function findValues(n, capacity, kS) { +function findValues(n, capacity, kS, weights, values) { let i = n; let k = capacity; // console.log('Items that are part of the solution:'); @@ -7,8 +7,8 @@ function findValues(n, capacity, kS) { // console.log( // item ' + i + ' can be part of solution w,v: ' + weights[i - 1] + ',' + values[i - 1] // ); + k -= weights[i-1] i--; - k -= kS[i][k]; } else { i--; } From c772d3f2c6f6e2e8421a09121427d9c29af6c2c4 Mon Sep 17 00:00:00 2001 From: wangpeijun Date: Sat, 5 Jun 2021 12:05:28 +0800 Subject: [PATCH 2/2] fix matrix-chain-multiplication.js --- .../dynamic-programing/matrix-chain-multiplication.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/algorithms/dynamic-programing/matrix-chain-multiplication.js b/src/js/algorithms/dynamic-programing/matrix-chain-multiplication.js index 143f5772..87a736b6 100644 --- a/src/js/algorithms/dynamic-programing/matrix-chain-multiplication.js +++ b/src/js/algorithms/dynamic-programing/matrix-chain-multiplication.js @@ -25,7 +25,7 @@ export function matrixChainOrder(p) { } } for (let l = 2; l < n; l++) { - for (let i = 1; i <= (n - l) + 1; i++) { + for (let i = 1; i <= n - l; i++) { const j = (i + l) - 1; m[i][j] = Number.MAX_SAFE_INTEGER; for (let k = i; k <= j - 1; k++) {