forked from Jack-Lee-Hiter/AlgorithmsByPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path322. Coin Change
More file actions
41 lines (37 loc) · 1.44 KB
/
322. Coin Change
File metadata and controls
41 lines (37 loc) · 1.44 KB
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
'''
You are given coins of different denominations and a total amount of money amount. Write a function to compute 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.
Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)
Example 2:
coins = [2], amount = 3
return -1.
Note:
You may assume that you have an infinite number of each kind of coin.
'''
class Solution(object):
def coinChange(self, coins, amount):
#corner cases
if amount == 0:
return 0
if len(coins) == 1 and coins[0] > amount:
return -1
dp = [-1 for i in range(amount + 1)]
for i in range(1, amount + 1):
# if the value matches the coin
if i in coins:
dp[i] = 1
else:
minV = sys.maxsize
# since the size of coins are much less than the amount,
# we check if for every coin there could be a solution and find the minimum of that
for j in coins:
remain = i - j
# -1 means there is no solution, so we don't need to check if dp[i] is -1
if remain > 0 and dp[remain] != -1:
minV = min(minV, dp[remain])
if minV ==sys.maxsize:
dp[i] = -1
else:
dp[i] = minV + 1
return dp[-1]