Skip to content

Commit 14b96a8

Browse files
committed
Knapsack problem
1 parent e57af49 commit 14b96a8

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

src/bitwise-operator-permissions/bitwise-operator-permissions.js

Whitespace-only changes.

src/bitwise-operator-permissions/bitwise-operator-permissions.test.js

Whitespace-only changes.

src/kanpsack/knapsack.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Problem: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible
2+
// Time complexity:
3+
4+
const items = [
5+
{ id: "a", weight: 8, value: 7 },
6+
{ id: "b", weight: 4, value: 5 },
7+
{ id: "c", weight: 2, value: 3 },
8+
{ id: "d", weight: 7, value: 5 },
9+
];
10+
const maxWeight = 11;
11+
12+
const getBag = (items, maxWeight, itemIndex) => {
13+
if (maxWeight === 0 || itemIndex < 0) {
14+
return {
15+
items: [],
16+
value: 0,
17+
weight: 0,
18+
};
19+
}
20+
21+
if (maxWeight < items[itemIndex].weight) {
22+
return getBag(items, maxWeight, itemIndex - 1);
23+
}
24+
25+
const bagWithItem =
26+
itemIndex &&
27+
getBag(items, maxWeight - items[itemIndex].weight, itemIndex - 1);
28+
29+
const bagWithoutItem =
30+
itemIndex &&
31+
getBag(items, maxWeight - items[itemIndex].weight, itemIndex - 1);
32+
33+
const valueWithItem = bagWithItem.value + items[itemIndex].value;
34+
35+
const valueWithoutItem = bagWithoutItem.value;
36+
37+
if (valueWithItem > valueWithoutItem) {
38+
const updatedBag = {
39+
items: bagWithItem.items.concat(items[itemIndex]),
40+
value: (bagWithItem.value += items[itemIndex].value),
41+
weight: (bagWithItem.weight += items[itemIndex].weight),
42+
};
43+
return updatedBag;
44+
} else {
45+
return bagWithoutItem;
46+
}
47+
};
48+
49+
export default getBag;

src/kanpsack/knapsack.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import getBag from "./knapsack";
2+
3+
describe("Knapsack", () => {
4+
test("Knapsack one", () => {
5+
const items = [
6+
{ id: "a", weight: 8, value: 7 },
7+
{ id: "b", weight: 4, value: 5 },
8+
{ id: "c", weight: 2, value: 3 },
9+
{ id: "d", weight: 7, value: 5 },
10+
];
11+
const maxWeight = 11;
12+
const result = {
13+
items: [
14+
{ id: "c", value: 3, weight: 2 },
15+
{ id: "d", value: 5, weight: 7 },
16+
],
17+
value: 8,
18+
weight: 9,
19+
};
20+
expect(getBag(items, maxWeight, items.length - 1)).toStrictEqual(
21+
result
22+
);
23+
});
24+
});

src/power-of-two/power-of-two.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const isPowerOfTwo = (num) => {
1111
// Utilizing bitwise operators
1212
// Binary number: 128 64 32 16 8 4 2 1
1313
// Converting number to binary number - using subtractions 75 -> 01001011
14+
// Time complexity: O(1)
1415
export const isPowerOfTwoBitwise = (num) => {
1516
return num & (num - 1) ? false : true;
1617
};
17-

0 commit comments

Comments
 (0)