Skip to content

Commit 1125a7c

Browse files
committed
leetcode.com 322. Coin Change
문제 링크: https://leetcode.com/problems/coin-change/description/
1 parent ed6b54d commit 1125a7c

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List
2+
import sys
3+
4+
5+
class Solution:
6+
def coinChange(self, coins: List[int], amount: int) -> int:
7+
dp: List[List[int]] = [[sys.maxsize for _ in range(amount + 1)] for _ in range(len(coins))]
8+
9+
for i in range(len(coins)):
10+
dp[i][0] = 0
11+
12+
for idx_coin in range(len(coins)):
13+
for idx in range(1, amount + 1):
14+
if idx_coin > 0:
15+
dp[idx_coin][idx] = min(dp[idx_coin][idx], dp[idx_coin - 1][idx])
16+
17+
idx_prev = idx - coins[idx_coin]
18+
if idx_prev >= 0:
19+
dp[idx_coin][idx] = min(dp[idx_coin][idx], dp[idx_coin][idx_prev] + 1)
20+
21+
if dp[len(coins) - 1][amount] == sys.maxsize:
22+
return -1
23+
24+
return dp[len(coins) - 1][amount]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from unittest import TestCase
2+
from main import Solution
3+
4+
5+
class TestSolution(TestCase):
6+
def test1_coin_change(self):
7+
sol = Solution()
8+
self.assertEqual(3, sol.coinChange([1,2,5], 11))
9+
10+
def test1_coin_change(self):
11+
sol = Solution()
12+
self.assertEqual(-1, sol.coinChange([2], 3))
13+
14+
def test1_coin_change(self):
15+
sol = Solution()
16+
self.assertEqual(0, sol.coinChange([1], 0))
17+

0 commit comments

Comments
 (0)