I am starting to write and understand coin change problem and couldn't get intuition so I have started to write a brute force solution. I want to understand the brute force solution before moving to memoization.
coins = [2, 3, 7]
change = 12
def coin_change(c):
print(c)
if c <= 0:
return 0
else:
for i in coins:
if c - i >= 0:
coin_change(c - i)
coin_change(change)
It prints out couple of changes left but I don't know how to store each path in an array.
I also want to understand how recursion can be used to track path. I can think of adding an extra argument in coin_change but maybe there is an alternate way.
I am confused about its complexity. It has a number of coins call at every step but many online resources mention it's 2n.
Edit: Please note that the coin supply is unlimited, The answer is 4
[2, 2, 2, 2, 2, 2]
[3, 3, 3, 3]
[2, 2, 2, 3, 3]
[2, 3, 7]