Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Dynamic Programming solution in Clear category for [old] Making Change by Sim0000
"use strict";
function makingChange(price, denominations) {
let table = new Array(price + 1).fill(price + 1);
table[0] = 0;
for(let coin of denominations){
for(let i = coin; i <= price; i++){
table[i] = Math.min(table[i], table[i - coin] + 1);
}
}
return table[price] <= price ? table[price] : undefined;
}
var assert = require('assert');
if (!global.is_checking) {
console.log('Example:');
console.log(makingChange(8, [1, 3, 5]));
// These "asserts" are used for self-checking and not for an auto-testing
assert.equal(makingChange(8, [1, 3, 5]), 2);
assert.equal(makingChange(12, [1, 4, 5]), 3)
console.log("Coding complete? Click 'Check' to earn cool rewards!");
}
May 31, 2019
Comments: