-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCoin_Change.py
27 lines (24 loc) · 902 Bytes
/
Coin_Change.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
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
n = len(coins)
tmp = set(coins)
dp = [[-1] * (amount + 1) for _ in range(n)]
for i in range(1, amount + 1):
quotient, remainder = divmod(i, coins[0])
if remainder == 0:
dp[0][i] = quotient
else:
dp[0][i] = -1
for i in range(n):
dp[i][0] = 0
for i in range(1, n):
for j in range(1, amount + 1):
use = -1 if j - coins[i] < 0 else dp[i][j - coins[i]]
not_use = dp[i - 1][j]
if use != -1 and not_use != -1:
dp[i][j] = min(use + 1, not_use)
elif use == -1:
dp[i][j] = not_use
else:
dp[i][j] = use + 1
return dp[-1][-1]