Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!