Skip to content

Commit 6ac8985

Browse files
committed
Add min change problem
1 parent 03bb9b1 commit 6ac8985

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/others/min-coins-change.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
(function (exports) {
2+
'use strict';
3+
4+
/**
5+
* Returns the minimum number of coins from given set,
6+
* which sum equals to given change. This is famous
7+
* problem from the dymanic programming:
8+
* https://en.wikipedia.org/wiki/Change-making_problem
9+
*
10+
* @public
11+
* @module others/minCoinsChange
12+
*
13+
* @example
14+
*
15+
* var minCoinsChange =
16+
* require('path-to-algorithms/src/others/min-coins-change')
17+
* .minCoinsChange;
18+
* var coins = minCoinsChange([1, 2, 3], 5); // [ 2, 3 ]
19+
*
20+
* @param {Array} coins The sorted list of the coins used for the change.
21+
* @param {Number} change The change, which should be returned.
22+
* @return Array which contains the minimum coins from the given
23+
* list, required for the change.
24+
*/
25+
function minCoinsChange(coins, change) {
26+
var minChange = [[0]];
27+
if (coins.indexOf(change) >= 0) {
28+
return [change];
29+
}
30+
for (var i = 1; i <= change; i += 1) {
31+
var current = null;
32+
for (var j = 0; j < coins.length && coins[j] <= change; j += 1) {
33+
for (var k = 0; k < minChange.length; k += 1) {
34+
if (k + coins[j] === i &&
35+
(!current || minChange[k].length + 1 < current.length)) {
36+
minChange[i] = minChange[k].concat([coins[j]]);
37+
}
38+
}
39+
}
40+
}
41+
var result = minChange[change];
42+
if (!result) {
43+
return undefined;
44+
}
45+
return result.slice(1);
46+
}
47+
48+
exports.minCoinsChange = minCoinsChange;
49+
50+
})(typeof window === 'undefined' ? module.exports : window);

test/others/min-coins-sum.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
var minCoinsChange =
4+
require('../../src/others/min-coins-change.js').minCoinsChange;
5+
6+
describe('Change making problem', function () {
7+
it('should be defined', function () {
8+
expect(minCoinsChange).toBeDefined();
9+
});
10+
11+
it('should work for 0 change', function () {
12+
expect(minCoinsChange([1, 2], 0)).toEqual([]);
13+
});
14+
15+
it('should work for change equals to array element', function () {
16+
expect(minCoinsChange([1, 2], 1)).toEqual([1]);
17+
});
18+
19+
it('should return the minimum amount of coins', function () {
20+
expect(minCoinsChange([1], 2)).toEqual([1, 1]);
21+
expect(minCoinsChange([1, 2], 3)).toEqual([1, 2]);
22+
// [2, 3, 2, 3] or [1, 3, 3, 3]
23+
expect(minCoinsChange([1, 2, 3], 10).length).toEqual(4);
24+
});
25+
26+
it('should return undefined for combination, which is not possible',
27+
function () {
28+
expect(minCoinsChange([1, 2, 3], 0.5)).not.toBeDefined();
29+
});
30+
});

0 commit comments

Comments
 (0)