Skip to content

Commit da5a28e

Browse files
🚀 perf(integerKnapsack): Cache v[i].
1 parent 4c137d8 commit da5a28e

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/integerKnapsack.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ const integerKnapsack = (v, w, n, W, m = new w.constructor(W + 1).fill(0)) => {
77
assert(m.length >= W + 1);
88
for (let i = 0; i < n; ++i) {
99
const x = w[i];
10+
const y = v[i];
1011
assert(Number.isInteger(x) && x >= 0 && x <= W);
1112
const s = W - x;
1213
for (let j = 0; j <= s; ++j) {
13-
m[j] = Math.max(m[j], m[j + x] + v[i]);
14+
m[j] = Math.max(m[j], m[j + x] + y);
1415
}
1516
}
1617

0 commit comments

Comments
 (0)