Created
February 26, 2019 20:31
-
-
Save morenoh149/302423f755817a8d3347570c032e7316 to your computer and use it in GitHub Desktop.
Dynamic programming example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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