mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
Added 2 Base tests and 5 main tests for CoinChange Problem. Refactored the code and removed the Memoized approach as it was not necessary
This commit is contained in:
@ -15,28 +15,3 @@ export const change = (coins, amount) => {
|
||||
}
|
||||
return combinations[amount]
|
||||
}
|
||||
function minimumCoins (coins, amount) {
|
||||
// minimumCoins[i] will store the minimum coins needed for amount i
|
||||
const minimumCoins = new Array(amount + 1).fill(0)
|
||||
|
||||
minimumCoins[0] = 0
|
||||
|
||||
for (let i = 1; i < amount + 1; i++) {
|
||||
minimumCoins[i] = Number.MAX_SAFE_INTEGER
|
||||
}
|
||||
for (let i = 1; i < amount + 1; i++) {
|
||||
for (let j = 0; j < coins.length; j++) {
|
||||
const coin = coins[j]
|
||||
if (coin <= i) {
|
||||
const subRes = minimumCoins[i - coin]
|
||||
if (
|
||||
subRes !== Number.MAX_SAFE_INTEGER &&
|
||||
subRes + 1 < minimumCoins[i]
|
||||
) {
|
||||
minimumCoins[i] = subRes + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return minimumCoins[amount]
|
||||
}
|
||||
|
Reference in New Issue
Block a user