Can't understand why this solution is wrong

My solution:

def calculate(int_coin_price):
    possible_price_splitted = (int_coin_price//2) + (int_coin_price//3) + (int_coin_price//4)
   
    final_result = possible_price_splitted if possible_price_splitted>int_coin_price  else int_coin_price

    return final_result
  
while True:
    try:
        
        coin_price = int(input())
        
        print(calculate(coin_price))
        
    except EOFError:
        break

A similar solution I found:

dict1 = {0:0}

def calculate(coinprice):
    if coinprice in dict1:
        return dict1[coinprice]
    else:
        value = calculate(coinprice//2) +calculate(coinprice//3) +calculate(coinprice//4)
        if value > coinprice:
            dict1[coinprice] = value
            return value
        else:
            dict1[coinprice] =  coinprice
            return dict1[coinprice]
        
        
        
while True:
    try:
        coin_price = int(input())
    
        print(calculate(coin_price))

    except EOFError:
        break

this one is correct and I don’t understand the difference. I can see there is a dictionary but don’t know if this difference is the one that makes it accepted.

The smallest n for which your solution gets the wrong answer is n=24 (it’s possible to get 27 dollars out of it).

2 Likes

Can you explain why? Is this because each number on the equation (n//2, n//3, n//4) has to also be converted to get the most value?

Yes. We should recursively apply the same function on each of (n//2, n//3, n//4).

You’ll know why it is used once you make the above changes and run your code to get TLE :stuck_out_tongue_winking_eye:

2 Likes

Oh ok got it!! Thanks this is solved now…