Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions dynamic_programming/coin_change_II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List

Check failure on line 1 in dynamic_programming/coin_change_II.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

dynamic_programming/coin_change_II.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 1 in dynamic_programming/coin_change_II.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

dynamic_programming/coin_change_II.py:1:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 1 in dynamic_programming/coin_change_II.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

dynamic_programming/coin_change_II.py:1:1: N999 Invalid module name: 'coin_change_II'

class Solution:
def change(self, amount: int, coins: List[int]) -> int:

Check failure on line 4 in dynamic_programming/coin_change_II.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

dynamic_programming/coin_change_II.py:4:42: UP006 Use `list` instead of `List` for type annotation
cache = {}
def dfs(i,a):
if a == amount:
return 1
if a > amount:
return 0
if i == len(coins):
return 0
if (i,a) in cache:
return cache[(i,a)]
cache[(i,a)] = dfs(i,a + coins[i]) +dfs(i+1, a)
return cache[(i,a)]
return dfs(0,0)

sol = Solution()
# for example
print(sol.change(5, [1,2,5])) # Output: 4
Loading