Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
Leetcode 322: Coin change with 2 programming languages produces 2 different results

I am trying to solve Leetcode Problem 322. Here's the description quoted from the site.

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 have written 2 recursive solutions, 1 in Python and 1 in Javascript. For some reason, the one in Javascript doesn’t produce the correct values for the same input, while the Python one always do.

Would like to ask if anyone knows what could be the reason for the difference in output. I tried with the following test cases:

coins = [1,2,5], amount = 11 => Expected: 3 
coins = [1,2], amount = 2 => Expected: 1

Here's the code I've written in the respective languages.

Javascript

var coinChange = function(coins, amount) {
    coins = coins.sort((a,b) => b -a )
    ans = helper(coins, amount, 0)
    if (ans >= Number.MAX_VALUE) {
        return -1
    }
    return ans;
};


function helper(coins, amount, pos) {
    if (pos >= coins.length || amount < 0) {
        return Number.MAX_VALUE; 
    } else if (amount === 0) {
        return 0; 
    }

    left = helper(coins, amount - coins[pos], pos) + 1
    right = helper(coins, amount, pos + 1)

    return Math.min(left, right)
}   

Using the above 2 test cases, it gets both test cases wrong.

coins = [1,2,5], amount = 11 => Expected: 3, gets 2
coins = [1,2], amount = 2 => Expected: 1, gets 2

Python

def coinChange(coins, amount):
    coins = sorted(coins, reverse = True)    
    ans = helper(coins, amount, 0)
    if (ans >= float("inf")):
        return -1
    return ans

def helper(coins, amount, pos):
    if (pos >= len(coins) or amount < 0):
        return float("inf")
    elif (amount == 0):
        return 0
    left = helper(coins, amount - coins[pos], pos) + 1
    right = helper(coins, amount, pos + 1)

    return min(left, right)

Using the above 2 test cases, it gets both tests correct.

coins = [1,2,5], amount = 11 => Expected: 3, gets 3
coins = [1,2], amount = 2 => Expected: 1, gets 1
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The Javascript code gets the expected answer by adding let to the return values to each of the recursive calls.

For example, left = helper(coins, amount - coins[pos], pos) + 1 is changed to

let left = helper(coins, amount - coins[pos], pos) + 1

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda