Skip to content

Commit fa4a499

Browse files
committed
feat: [Week 04-5] solve coin-change
1 parent 6bbe081 commit fa4a499

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

coin-change/Chaedie.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
직접 풀지 못해 알고달레 풀이를 참고했습니다. https://www.algodale.com/problems/coin-change/
3+
4+
Solution:
5+
1) BFS를 통해 모든 동전을 한번씩 넣어보며 amount와 같아지면 return
6+
7+
(c: coins의 종류 갯수, a: amount)
8+
Time: O(ca)
9+
Space: O(a)
10+
"""
11+
12+
13+
class Solution:
14+
def coinChange(self, coins: List[int], amount: int) -> int:
15+
q = deque([(0, 0)]) # (동전 갯수, 누적 금액)
16+
visited = set()
17+
18+
while q:
19+
count, total = q.popleft()
20+
if total == amount:
21+
return count
22+
if total in visited:
23+
continue
24+
visited.add(total)
25+
for coin in coins:
26+
if total + coin <= amount:
27+
q.append((count + 1, total + coin))
28+
return -1

0 commit comments

Comments
 (0)