Skip to content

Commit c70a504

Browse files
committed
Solve coin-change with python
1 parent 0160a7b commit c70a504

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

coin-change/bemelon.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
# Space complexity: O(n)
3+
# Time complexity: O(n * m)
4+
# - n: amount
5+
# - m: len(coins)
6+
def coinChange(self, coins: list[int], amount: int) -> int:
7+
INIT_VALUE = 999999999
8+
dp = [INIT_VALUE] * (amount + 1)
9+
dp[0] = 0
10+
11+
for x in range(1, amount + 1):
12+
for coin in coins:
13+
if x - coin >= 0:
14+
dp[x] = min(dp[x], dp[x - coin] + 1)
15+
16+
return dp[amount] if dp[amount] != INIT_VALUE else -1

0 commit comments

Comments
 (0)