Skip to content

Commit

Permalink
solve issue llipio#56 find minimum number of coins
Browse files Browse the repository at this point in the history
  • Loading branch information
yjlim5 committed Jun 5, 2017
1 parent 7c1ecfd commit 1b04108
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
20 changes: 20 additions & 0 deletions solutions/56.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// coin denomination problem

const solution = (arr, total) => {
if (total === 0) {
return 0;
}

// set minimum number of coins to the total divided by smallest denomination
let min = Math.floor(total / Math.min(...arr));
arr.forEach((denom) => {
if (total - denom >= 0) {
min = Math.min(1 + solution(arr, total - denom), min);
}
});
return min;
};

module.exports = {
solution
};
19 changes: 19 additions & 0 deletions test/56.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const expect = require('chai').expect;
const solution = require('../solutions/56.js').solution;
// solution = require('../yourSolution').solution;

describe('return smallest number of coins required to return the change', () => {
it('simplest case [1],total=1', () => {
expect(solution([1,2],2)).to.equal(1);
});
it('simple case [1,2],total=2', () => {
expect(solution([1,2],2)).to.equal(1);
});
it('simple case [1,2], total=4', () => {
expect(solution([1,2],4)).to.equal(2);
});
it('hard case [2,3,6,7], total=12', () => {
expect(solution([2,3,6,7],12)).to.equal(2);
});
});

0 comments on commit 1b04108

Please sign in to comment.