Skip to content

Instantly share code, notes, and snippets.

@morenoh149
Created Feb 26, 2019
Embed
What would you like to do?
Dynamic programming example
// The following was translated from python.
// See https://codility.com/media/train/15-DynamicProgramming.pdf
const dynamicCoinChanging = (coins, target) => {
let n = coins.length;
let dp = [0];
for (let i=0; i < target; i++) {
dp.push(Number.POSITIVE_INFINITY);
}
for (let i=0; i <= n; i++) {
for (let j=coins[i - 1]; j <= target; j++) {
dp[j] = Math.min(dp[j - coins[i - 1]] + 1, dp[j]);
}
}
return dp;
}
console.log(dynamicCoinChanging([1, 3, 4], 6));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment