forked from xuwd11/Coursera-Bioinformatics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
31_01_DPChage.py
50 lines (43 loc) · 1.46 KB
/
31_01_DPChage.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
46
47
48
49
50
# python3
import sys
'''
Solve the Change Problem. The DPChange pseudocode is reproduced below for your convenience.
Input: An integer money and an array Coins = (coin1, ..., coind).
Output: The minimum number of coins with denominations Coins that changes money.
Sample Input:
40
50,25,20,10,5,1
Sample Output:
2
Pseudocode:
DPChange(money, Coins)
MinNumCoins(0) ← 0
for m ← 1 to money
MinNumCoins(m) ← ∞
for i ← 1 to |Coins|
if m ≥ coini
if MinNumCoins(m - coini) + 1 < MinNumCoins(m)
MinNumCoins(m) ← MinNumCoins(m - coini) + 1
output MinNumCoins(money)
'''
class MinNumCoins:
def __init__(self):
self._input()
print(self.DPChange(self.money, self.coins))
def _input(self):
data = sys.stdin.read().strip().split()
self.money = int(data[0])
self.coins = [int(c) for c in data[1].split(',')]
def DPChange(self, money, coins):
minNumCoins = [0]
for m in range(1, money + 1):
globalMin = float('inf')
for coin in coins:
if m >= coin:
currMin = minNumCoins[m-coin] + 1
if currMin < globalMin:
globalMin = currMin
minNumCoins.append(globalMin)
return minNumCoins[money]
if __name__ == "__main__":
MinNumCoins()