Created
November 8, 2021 17:13
-
-
Save adityak74/8010fea67a3a62e416d604c7c3ad14e2 to your computer and use it in GitHub Desktop.
Find minimum cost DP problem Hackerrank
This file contains 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
function calculateAmount(prices) { | |
let dp = []; | |
dp[0] = prices[0]; | |
let totalCost = prices[0]; | |
for (let i = 1; i < prices.length; i += 1) { | |
if (prices[i] - dp[i-1] < 0) { | |
totalCost += 0; | |
dp[i] = Math.min(prices[i], dp[i-1]); | |
} | |
else { | |
totalCost += prices[i] - dp[i-1]; | |
dp[i] = Math.min(prices[i], dp[i-1]); | |
} | |
} | |
return totalCost; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment