Skip to content

Instantly share code, notes, and snippets.

@frobnitzem
frobnitzem / knapsack.js
Last active November 27, 2020 10:58 — forked from danwoods/knapsack.js
Knapsack algorithm in JavaScript
// Solve the knapsack problem using recursive descent.
// This wraps the actual solver below with a nice interface.
// It also handles non-integer cost, but then the complexity
// scales with the precision of the cost!
//
// obj is an object of named [cost, benefit] pairs, e.g.
// { banana: [5, 33],
// apple: [1, 12],
// kiwi: [1, 7]
// }