Skip to content

Commit 55f03bb

Browse files
authored
Merge pull request #1 from Drag0nop/Drag0nop-patch-1
Create coin_change_II.py
2 parents a71618f + 816f99b commit 55f03bb

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
3+
class Solution:
4+
def change(self, amount: int, coins: List[int]) -> int:
5+
cache = {}
6+
def dfs(i,a):
7+
if a == amount:
8+
return 1
9+
if a > amount:
10+
return 0
11+
if i == len(coins):
12+
return 0
13+
if (i,a) in cache:
14+
return cache[(i,a)]
15+
cache[(i,a)] = dfs(i,a + coins[i]) +dfs(i+1, a)
16+
return cache[(i,a)]
17+
return dfs(0,0)
18+
19+
sol = Solution()
20+
# for example
21+
print(sol.change(5, [1,2,5])) # Output: 4

0 commit comments

Comments
 (0)