-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathminimum_coins.py
45 lines (35 loc) · 1.24 KB
/
minimum_coins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Given a list of coin denominations and an amount,
this program calculates the minimum number of coins needed to make up that amount.
Example :
coins = [1, 2, 5, 10, 20, 50, 100, 500, 2000],
amount = 121,
The minimum number of coins would be 3 (100 + 20 + 1).
This problem can be solved using the concept of "GREEDY ALGORITHM".
We start with the largest denomination of coins and use as many of those,
as possible before moving to the next largest denomination.
This process continues until the entire amount has been made up of coins.
"""
def min_coins(coins: list[int], amount: int = 0) -> tuple:
"""
>>> min_coins([1, 2, 5, 10, 20, 50, 100, 500, 2000],121)
(3, [100, 20, 1])
>>> min_coins([1, 2, 5, 10, 20, 50, 100],343)
(7, [100, 100, 100, 20, 20, 2, 1])
>>> min_coins([1,2,5,10],0)
(0, [])
"""
coins.sort(reverse=True)
count: int = 0
coins_list: list[int] = []
for coin in coins:
if coin <= amount:
while coin <= amount:
count += 1
coins_list.append(coin)
amount -= coin
return count, coins_list
if __name__ == "__main__":
import doctest
doctest.testmod()
print(f"{min_coins([1, 2, 5, 10, 20, 50, 100, 500, 2000],121)}")