-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathknapsack.js
More file actions
36 lines (30 loc) · 1.01 KB
/
knapsack.js
File metadata and controls
36 lines (30 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/////////////////////////////////////////////////////////////
// Thief's knapsack
// --------------------------
// A Thief has a knapsack that can hold X lbs of stolen goods
// Each stolen good is worth a certain amount of cash, but
// the stolen good also weighs a certain weight. This means that
// the thief has to pick an optimal combination of items!
//
// The Thief can't pick the same item twice.
//
// What is the maximum worth of goods that the thief can steal?
//
/////////////////////////////////////////////////////////////
var knapsack = function (weights, maxWeight) {
// TODO: Implement!
};
/////////////////////////////////////////////////////////////
// TEST CASES
/////////////////////////////////////////////////////////////
//Keys are weights, values are stolen good's values.
var weights = {
10: 40,
20: 55,
1: 1,
5: 50,
50: 147,
};
console.log('Pass? ', knapsack(weights, 31) === 106);
console.log('Pass? ', knapsack(weights, 50) === 147);
console.log('Pass? ', knapsack(weights, 60) === 198);