forked from llipio/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solve issue llipio#56 find minimum number of coins
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
|