1

was doing coin change problem as an assignment(didnt know before), after came with this solution i checked the solutions available couldnt find a similar one like this, just curious. removing the element from the list is O(N) so seems this also O(N^2) right?

def rec_coin(target,coins):
    if target == 0 or coins == []:
        return 0

    max_coin = max(coins)
    mod = target % max_coin
    coin_count = (target-mod)/ max_coin
    coins.remove(max_coin)

    return int(coin_count) + rec_coin(mod, coins)

rec_coin(23,[1,5,10,25])
Ntwobike
  • 111
  • 1

0 Answers0