Skip to content

Commit f0f30b6

Browse files
author
Alagappan Maruthappan
committed
coin change problem
1 parent 102f15e commit f0f30b6

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

coin_change.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# https://leetcode.com/problems/coin-change/description/
2+
3+
4+
class Solution:
5+
def coinChange(self, coins, amount):
6+
"""
7+
:type coins: List[int]
8+
:type amount: int
9+
:rtype: int
10+
"""
11+
if amount == 0:
12+
return 0
13+
DP = [amount + 1] * (amount + 1)
14+
DP[0] = 0
15+
for i in range(1, amount + 1):
16+
for coin in coins:
17+
if coin <= i:
18+
DP[i] = min(DP[i], DP[i-coin] + 1)
19+
return DP[amount] if DP[amount] != amount + 1 else -1
20+
21+
22+
assert Solution().coinChange([1, 2, 5], 11) == 3

0 commit comments

Comments
 (0)