11from typing import List
22
3+ cache = {}
4+
35
46def ways_to_make_change (total : int ) -> int :
57 """
6- Given access to coins with the values 1, 2, 5, 10, 20, 50, 100, 200, returns a count of all of the ways to make the passed total value.
7-
8- For instance, there are two ways to make a value of 3: with 3x 1 coins, or with 1x 1 coin and 1x 2 coin.
8+ Given access to coins with the values 1, 2, 5, 10, 20, 50, 100, 200,
9+ returns a count of all of the ways to make the passed total value.
910 """
10- return ways_to_make_change_helper (total , [200 , 100 , 50 , 20 , 10 , 5 , 2 , 1 ])
11+ cache .clear ()
12+ return ways_to_make_change_helper (
13+ total ,
14+ [200 , 100 , 50 , 20 , 10 , 5 , 2 , 1 ]
15+ )
1116
1217
1318def ways_to_make_change_helper (total : int , coins : List [int ]) -> int :
14- """
15- Helper function for ways_to_make_change to avoid exposing the coins parameter to callers.
16- """
19+ key = (total , tuple (coins ))
20+
21+ if key in cache :
22+ return cache [key ]
23+
1724 if total == 0 or len (coins ) == 0 :
1825 return 0
1926
2027 ways = 0
28+
2129 for coin_index in range (len (coins )):
2230 coin = coins [coin_index ]
2331 count_of_coin = 1
32+
2433 while coin * count_of_coin <= total :
2534 total_from_coins = coin * count_of_coin
35+
2636 if total_from_coins == total :
2737 ways += 1
2838 else :
29- intermediate = ways_to_make_change_helper (total - total_from_coins , coins = coins [coin_index + 1 :])
30- ways += intermediate
39+ ways += ways_to_make_change_helper (
40+ total - total_from_coins ,
41+ coins [coin_index + 1 :]
42+ )
43+
3144 count_of_coin += 1
32- return ways
45+
46+ cache [key ] = ways
47+ return ways
0 commit comments