Skip to content

Commit

Permalink
🚀 perf(integerKnapsack): Cache v[i].
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Oct 9, 2020
1 parent 4c137d8 commit da5a28e
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/integerKnapsack.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ const integerKnapsack = (v, w, n, W, m = new w.constructor(W + 1).fill(0)) => {
assert(m.length >= W + 1);
for (let i = 0; i < n; ++i) {
const x = w[i];
const y = v[i];
assert(Number.isInteger(x) && x >= 0 && x <= W);
const s = W - x;
for (let j = 0; j <= s; ++j) {
m[j] = Math.max(m[j], m[j + x] + v[i]);
m[j] = Math.max(m[j], m[j + x] + y);
}
}

Expand Down

0 comments on commit da5a28e

Please sign in to comment.