Merge pull request #1921 from RafaelTaranto/fix/coin-change-with-empty-box

LAM-1496 fix: coin change with empty box
This commit is contained in:
Rafael Taranto 2025-07-23 14:37:46 +01:00 committed by GitHub
commit bc4d41951a

View file

@ -9,12 +9,12 @@
*/
const prepare_denominations = denominations =>
JSON.parse(JSON.stringify(denominations))
.sort(([d1], [d2]) => d2 - d1)
.filter(([, count]) => count > 0)
.sort(([d1], [d2]) => d1 - d2)
.reduce(
([csum, denoms], [denom, count]) => {
csum += denom * count
denoms.push({ denom, count, csum })
return [csum, denoms]
return [csum, [{ denom, count, csum }].concat(denoms)]
},
[0, []],
)[1] /* ([csum, denoms]) => denoms */