We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6bbe081 commit fa4a499Copy full SHA for fa4a499
coin-change/Chaedie.py
@@ -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