Skip to content

Commit 08cfbf7

Browse files
committed
coin change solution
1 parent 56cd324 commit 08cfbf7

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

coin-change/devyejin.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# # 중복조합
2+
# class Solution:
3+
# def coinChange(self, coins: List[int], amount: int) -> int:
4+
#
5+
# def backtrack(current, total):
6+
# if total == amount:
7+
# return len(current)
8+
#
9+
# if total > amount:
10+
# return float('inf')
11+
#
12+
# min_count = float('inf')
13+
# for coin in coins:
14+
# current.append(coin)
15+
# result = backtrack(current, total + coin)
16+
# min_count = min(min_count, result)
17+
# current.pop()
18+
#
19+
# return min_count
20+
#
21+
# ans = backtrack([], 0)
22+
# return -1 if ans == float('inf') else ans
23+
from collections import deque
24+
from typing import List
25+
26+
class Solution:
27+
def coinChange(self, coins: List[int], amount: int) -> int:
28+
if amount == 0:
29+
return 0
30+
31+
queue = deque([(0, 0)])
32+
visited = set([0])
33+
34+
while queue:
35+
current_amount, count = queue.popleft()
36+
37+
for coin in coins:
38+
new_amount = current_amount + coin
39+
if new_amount == amount:
40+
return count + 1
41+
if new_amount < amount and new_amount not in visited:
42+
visited.add(new_amount)
43+
queue.append((new_amount, count + 1))
44+
45+
return -1
46+

0 commit comments

Comments
 (0)