I am trying to solve this problem:
Coin Change: https://leetcode.com/problems/coin-change/description/
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
I dont understand why this is giving wrong answer
<code>var coinChange = function (coins, amount) {
const helper = (coins, amount, index, step, cache )=>{
if (amount == 0) return step
if (index == coins.length || amount < 0) return Number.MAX_SAFE_INTEGER
let key = amount + "-" + index
if(cache[key] != undefined) return cache[key]
let result1 = helper(coins, amount - coins[index], index , step+1, cache);
result1 = result1 == Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER: 1 + result1
let result2 = helper(coins, amount, index + 1, step, cache)
cache[key] = Math.min(result1, result2)
const result = helper(coins, amount, 0 , 0, cache)
if (result == Number.MAX_SAFE_INTEGER) return -1
<code>var coinChange = function (coins, amount) {
const cache = {}
const helper = (coins, amount, index, step, cache )=>{
if (amount == 0) return step
if (index == coins.length || amount < 0) return Number.MAX_SAFE_INTEGER
let key = amount + "-" + index
if(cache[key] != undefined) return cache[key]
let result1 = helper(coins, amount - coins[index], index , step+1, cache);
result1 = result1 == Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER: 1 + result1
let result2 = helper(coins, amount, index + 1, step, cache)
cache[key] = Math.min(result1, result2)
return cache[key]
}
const result = helper(coins, amount, 0 , 0, cache)
console.log(cache)
if (result == Number.MAX_SAFE_INTEGER) return -1
return result
};
</code>
var coinChange = function (coins, amount) {
const cache = {}
const helper = (coins, amount, index, step, cache )=>{
if (amount == 0) return step
if (index == coins.length || amount < 0) return Number.MAX_SAFE_INTEGER
let key = amount + "-" + index
if(cache[key] != undefined) return cache[key]
let result1 = helper(coins, amount - coins[index], index , step+1, cache);
result1 = result1 == Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER: 1 + result1
let result2 = helper(coins, amount, index + 1, step, cache)
cache[key] = Math.min(result1, result2)
return cache[key]
}
const result = helper(coins, amount, 0 , 0, cache)
console.log(cache)
if (result == Number.MAX_SAFE_INTEGER) return -1
return result
};
but this is giving right answer
<code>var coinChange = function (coins, amount) {
const helper = (coins, amount, index, step, cache )=>{
if (amount == 0) return step
if (index == coins.length || amount < 0) return Number.MAX_SAFE_INTEGER
let key = amount + "-" + index
if(cache[key] != undefined) return cache[key]
let result1 = helper(coins, amount - coins[index], index , cache);
result1 = result1 == Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER: 1 + result1
let result2 = helper(coins, amount, index + 1, step+ cache)
cache[key] = Math.min(result1, result2)
const result = helper(coins, amount, 0 , cache)
if (result == Number.MAX_SAFE_INTEGER) return -1
<code>var coinChange = function (coins, amount) {
const cache = {}
const helper = (coins, amount, index, step, cache )=>{
if (amount == 0) return step
if (index == coins.length || amount < 0) return Number.MAX_SAFE_INTEGER
let key = amount + "-" + index
if(cache[key] != undefined) return cache[key]
let result1 = helper(coins, amount - coins[index], index , cache);
result1 = result1 == Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER: 1 + result1
let result2 = helper(coins, amount, index + 1, step+ cache)
cache[key] = Math.min(result1, result2)
return cache[key]
}
const result = helper(coins, amount, 0 , cache)
if (result == Number.MAX_SAFE_INTEGER) return -1
return result
};
</code>
var coinChange = function (coins, amount) {
const cache = {}
const helper = (coins, amount, index, step, cache )=>{
if (amount == 0) return step
if (index == coins.length || amount < 0) return Number.MAX_SAFE_INTEGER
let key = amount + "-" + index
if(cache[key] != undefined) return cache[key]
let result1 = helper(coins, amount - coins[index], index , cache);
result1 = result1 == Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER: 1 + result1
let result2 = helper(coins, amount, index + 1, step+ cache)
cache[key] = Math.min(result1, result2)
return cache[key]
}
const result = helper(coins, amount, 0 , cache)
if (result == Number.MAX_SAFE_INTEGER) return -1
return result
};
can anyone please help me understand this because I believe I am struggling this because I lack basic concepts.