Skip to content

Instantly share code, notes, and snippets.

@morenoh149
Created February 26, 2019 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morenoh149/302423f755817a8d3347570c032e7316 to your computer and use it in GitHub Desktop.
Save morenoh149/302423f755817a8d3347570c032e7316 to your computer and use it in GitHub Desktop.
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