If you use dyanmic programming
you can only get the best result, you can achieve this by using a array to store the middle result of dynamic programming
, I have modified based on your dp version:
def change(coins, amount):
result = [amount+1] * (amount+1)
coins_results = [[] for _ in range(amount+1)]
result[0] = 0
for i in range(1, amount+1):
for coin in coins:
if i >= coin and result[i - coin] + 1 < result[i]:
result[i] = result[i-coin] + 1
coins_results[i] = coins_results[i-coin] + [coin]
if result[amount] == amount+1:
return []
return coins_results[amount]
test:
print(change([1, 2, 5, 8], 7))
print(change([2], 10))
output:
[5, 2]
[2, 2, 2, 2, 2]
here is a version to output all the result by backtracking
:
def change(coins, amount):
res = []
def backtrack(end, remain, cur_result):
if end < 0: return
if remain == 0:
res.append(cur_result)
return
if remain >= coins[end]:
backtrack(end, remain - coins[end], cur_result + [coins[end]])
backtrack(end - 1, remain, cur_result)
backtrack(len(coins) - 1, amount, [])
return res
test:
print(change([1, 2, 5, 8], 7))
print(change([2], 10))
output:
[[5, 2], [5, 1, 1], [2, 2, 2, 1], [2, 2, 1, 1, 1], [2, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1]]
[[2, 2, 2, 2, 2]]
Hope that helps you, and comment if you have further questions. : )