MCPcopy
hub / github.com/mgechev/javascript-algorithms / minCoinsChange

Function minCoinsChange

src/others/min-coins-change.js:25–44  ·  view source on GitHub ↗

* Returns the minimum number of coins from given set, * which sum equals to given change. This is famous * problem from the dynamic programming: * https://en.wikipedia.org/wiki/Change-making_problem * * @public * @module others/minCoinsChange * * @example * * v

(coins, change)

Source from the content-addressed store, hash-verified

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 for (var j = 0; j < coins.length && coins[j] <= change; j += 1) {
32 for (var k = 0; k < minChange.length; k += 1) {
33 if (k + coins[j] === i) {
34 minChange[i] = minChange[k].concat([coins[j]]);
35 }
36 }
37 }
38 }
39 var result = minChange[change];
40 if (!result) {
41 return undefined;
42 }
43 return result.slice(1);
44 }
45
46 exports.minCoinsChange = minCoinsChange;
47

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected