-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoinChange.py
43 lines (32 loc) · 1.24 KB
/
CoinChange.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
# Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
# You may assume that you have an infinite number of each kind of coin.
def coinChange(coins, amount):
def helper(amount, memo = {}):
if amount in memo:
return memo[amount]
if not amount:
return 0
if amount < 0:
return None
shortestCombination = float("inf")
for i in coins:
remainderCount = helper(amount - i)
if remainderCount != None and remainderCount + 1 < shortestCombination:
shortestCombination = remainderCount + 1
memo[amount] = shortestCombination
return shortestCombination
changeCount = helper(amount)
if changeCount == float("inf"):
return -1
return changeCount
# Test cases
coins = [1, 2, 5]
amount = 11
print(coinChange(coins, amount))
coins = [2]
amount = 3
print(coinChange(coins, amount))
coins = [1]
amount = 0
print(coinChange(coins, amount))