-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[solutions][dp] Add solution to Coin Change in JS.
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
TOP-DOWN DP SOLUTION | ||
*/ | ||
const CANNOT_PRODUCE_SPECIFIED_AMOUNT = -1; | ||
|
||
const coinChangeHelper = (coins, amount, cache) => { | ||
const amountInCache = cache.hasOwnProperty(amount); | ||
|
||
if (amountInCache) { | ||
return cache[amount]; | ||
} | ||
|
||
let fewestCoinsToMakeAmount = Infinity; | ||
|
||
coins.forEach((coinAmount) => { | ||
const remainingAmount = amount - coinAmount; | ||
|
||
if (remainingAmount < 0) return; | ||
|
||
const remainingCoinsRequired = coinChangeHelper( | ||
coins, | ||
remainingAmount, | ||
cache, | ||
); | ||
|
||
if (remainingCoinsRequired === CANNOT_PRODUCE_SPECIFIED_AMOUNT) return; | ||
|
||
fewestCoinsToMakeAmount = Math.min( | ||
fewestCoinsToMakeAmount, | ||
1 + remainingCoinsRequired, | ||
); | ||
}); | ||
|
||
if (fewestCoinsToMakeAmount === Infinity) { | ||
fewestCoinsToMakeAmount = CANNOT_PRODUCE_SPECIFIED_AMOUNT; | ||
} | ||
|
||
cache[amount] = fewestCoinsToMakeAmount; | ||
|
||
return cache[amount]; | ||
}; | ||
|
||
const coinChange = (coins, amount) => { | ||
const cache = {}; | ||
|
||
cache[0] = 0; | ||
|
||
return coinChangeHelper(coins, amount, cache); | ||
}; | ||
|
||
/* | ||
BOTTOM-UP DP SOLUTION | ||
*/ | ||
const CANNOT_PRODUCE_SPECIFIED_AMOUNT = -1; | ||
|
||
const coinChange = (coins, targetAmount) => { | ||
const cache = {}; | ||
|
||
cache[0] = 0; | ||
|
||
for (let curAmount = 1; curAmount <= targetAmount; curAmount++) { | ||
let fewestCoinsToMakeCurAmount = Infinity; | ||
|
||
coins.forEach((coinAmount) => { | ||
const remainingAmount = curAmount - coinAmount; | ||
|
||
if (remainingAmount < 0) return; | ||
|
||
// If `remainingAmount` is in `cache`, set `minCoinsToMakeRemainingAmount` to `cache[remainingAmount]. Otherwise, set it to `Infinity`. | ||
const remainingAmountInCache = cache.hasOwnProperty(remainingAmount); | ||
const minCoinsToMakeRemainingAmount = remainingAmountInCache | ||
? cache[remainingAmount] | ||
: Infinity; | ||
|
||
fewestCoinsToMakeCurAmount = Math.min( | ||
fewestCoinsToMakeCurAmount, | ||
1 + minCoinsToMakeRemainingAmount, | ||
); | ||
}); | ||
|
||
cache[curAmount] = fewestCoinsToMakeCurAmount; | ||
} | ||
|
||
const result = cache[targetAmount]; | ||
|
||
if (result === Infinity) { | ||
return CANNOT_PRODUCE_SPECIFIED_AMOUNT; | ||
} | ||
|
||
return result; | ||
}; |