algorithmpool
Version:
A pool of algorithms and data-structures for geeks
11 lines • 399 B
JavaScript
export const knapSack = (capacity, weights, values, n) => {
if (n === 0 || capacity === 0) {
return 0;
}
if (weights[n - 1] > capacity) {
return knapSack(capacity, weights, values, n - 1);
}
const a = values[n - 1] + knapSack(capacity - weights[n - 1], weights, values, n - 1);
const b = knapSack(capacity, weights, values, n - 1);
return a > b ? a : b;
}