Skip to content

Commit 1ef2cfd

Browse files
Coin Change Problem
1 parent 0d47186 commit 1ef2cfd

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Practice-Problems/CoinChangeProblem.py

+24
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,27 @@
1616
1717
With 1 coin being the minimum amount.
1818
'''
19+
20+
# Basic Recursion (Not Optimal)
21+
def rec_coin(target, coins):
22+
23+
# Default Value set to target
24+
min_coins = target
25+
26+
# Base case to check if target is in coin values list
27+
if target in coins:
28+
29+
return 1
30+
31+
else:
32+
33+
# For every coin value that is <= target
34+
for i in [c for c in coins if c <= target]:
35+
36+
# Add a coin count + Recursive
37+
num_coins = 1 + rec_coin(target - i, coins)
38+
39+
# Reset minimum if new num_coins < min_coins
40+
min_coins = min(num_coins, min_coins)
41+
42+
return min_coins

0 commit comments

Comments
 (0)