Skip to content

Commit c82f16f

Browse files
author
sejineer
committed
coin-change solution
1 parent 0dbfa7e commit c82f16f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

coin-change/sejineer.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import deque
2+
3+
class Solution:
4+
def coinChange(self, coins: List[int], amount: int) -> int:
5+
queue = deque([(0, 0)])
6+
vis = set()
7+
8+
while queue:
9+
total, count = queue.popleft()
10+
if total == amount:
11+
return count
12+
for coin in coins:
13+
nxt = total + coin
14+
if nxt > amount:
15+
continue
16+
if nxt in vis:
17+
continue
18+
vis.add(nxt)
19+
queue.append((nxt, count + 1))
20+
return -1

0 commit comments

Comments
 (0)