Skip to content

Commit 9e40a33

Browse files
committed
make changes
1 parent c90481b commit 9e40a33

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

‎Sprint-2/improve_with_caches/making_change/making_change.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import List
2+
cache = {}
23

34

45
def ways_to_make_change(total: int) -> int:
@@ -14,6 +15,11 @@ def ways_to_make_change_helper(total: int, coins: List[int]) -> int:
1415
"""
1516
Helper function for ways_to_make_change to avoid exposing the coins parameter to callers.
1617
"""
18+
key = (total, tuple(coins))
19+
20+
if key in cache:
21+
return cache[key]
22+
1723
if total == 0 or len(coins) == 0:
1824
return 0
1925

@@ -29,4 +35,7 @@ def ways_to_make_change_helper(total: int, coins: List[int]) -> int:
2935
intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:])
3036
ways += intermediate
3137
count_of_coin += 1
38+
39+
cache[key] = ways
3240
return ways
41+

0 commit comments

Comments
 (0)