Skip to content

Commit 18a711d

Browse files
committed
Get change
1 parent 79b8ada commit 18a711d

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

src/change-making/change-making.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Problem: Return minimum amount fo coins that add up to a given amount of money
2+
// Time complexity:
3+
4+
const getChange = (cents) => {
5+
const change = {};
6+
const coins = [100, 50, 20, 10, 5, 2, 1];
7+
coins.forEach((coin) => {
8+
if (cents >= coin) {
9+
const numOfCoins = Math.floor(cents / coin);
10+
change[coin] = numOfCoins;
11+
cents -= coin * numOfCoins;
12+
}
13+
});
14+
return change;
15+
};
16+
17+
export default getChange;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import getChange from "./change-making";
2+
3+
describe("Get change", () => {
4+
test("Get change", () => {
5+
expect(getChange(5)).toStrictEqual({ 5: 1 });
6+
});
7+
test("Get change", () => {
8+
expect(getChange(123)).toStrictEqual({ 1: 1, 100: 1, 2: 1, 20: 1 });
9+
});
10+
test("Get change", () => {
11+
expect(getChange(17)).toStrictEqual({ 10: 1, 2: 1, 5: 1 });
12+
});
13+
});

src/kanpsack/knapsack.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
// 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
22
// 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-
123
const getBag = (items, maxWeight, itemIndex) => {
134
if (maxWeight === 0 || itemIndex < 0) {
145
return {
@@ -47,3 +38,6 @@ const getBag = (items, maxWeight, itemIndex) => {
4738
};
4839

4940
export default getBag;
41+
42+
// 0-1 Knapsack problem:
43+

0 commit comments

Comments
 (0)