Skip to content

Commit

Permalink
issue llipio#56 use reduce function on array
Browse files Browse the repository at this point in the history
  • Loading branch information
yjlim5 committed Jun 5, 2017
1 parent ab1935c commit 7a2b68e
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions solutions/56.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ const solution = (arr, total) => {
if (total === 0) {
return 0;
}

let min = total;
arr.forEach((denom) => {
if (total - denom >= 0) {
min = Math.min(1 + solution(arr, total - denom), min);
}
});
return min;

return arr.reduce( (acc, denomination) => {
if (total - denomination >= 0) {
return Math.min(acc, 1 + solution(arr, total - denomination));
}
return acc;
}, total);
};

module.exports = {
solution
solution
};

0 comments on commit 7a2b68e

Please sign in to comment.