Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda