Skip to content

Commit 4334633

Browse files
committed
making_change
1 parent e1e32c7 commit 4334633

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

Sprint-2/improve_with_caches/making_change/making_change.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@ def ways_to_make_change_helper(total: int, coins: List[int]) -> int:
1616
"""
1717
if total == 0 or len(coins) == 0:
1818
return 0
19-
20-
ways = 0
21-
for coin_index in range(len(coins)):
22-
coin = coins[coin_index]
23-
count_of_coin = 1
24-
while coin * count_of_coin <= total:
25-
total_from_coins = coin * count_of_coin
26-
if total_from_coins == total:
27-
ways += 1
28-
else:
29-
intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:])
30-
ways += intermediate
31-
count_of_coin += 1
32-
return ways
19+
20+
ways = [0] * (total + 1)
21+
ways[0] = 1
22+
23+
for coin in coins:
24+
for amount in range(coin, total + 1):
25+
ways[amount] += ways[amount - coin]
26+
27+
return ways[total]

0 commit comments

Comments
 (0)