-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path[LeetCode] 322. Coin Change.py
122 lines (83 loc) · 2.25 KB
/
[LeetCode] 322. Coin Change.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [0] + [math.inf for _ in range(amount)]
for i in range(1, len(dp)):
for c in coins:
if i-c >= 0:
dp[i] = min(dp[i], dp[i-c]+1)
return dp[-1] if dp[-1] != math.inf else -1
'''
amount : 3
res : 2 (2+1), (1+1+1)
c
coins : [1, 2]
i
0. 1. 2. 3
dp : [0, 1, 1, 2]
1. DP
=> 4 ways
a) fibonacci
-
b) state machine
c) 2 strings
d) merge intervals (gaps)\
1. Problem is
- coins
- amount
- total amount of money
- impossible : -1
2. TC
tc1)
conis : [1, 2]
amo : 5
res : 3 (2 + 2 + 1)
tc2)
coins : [1]
amo : 5
res : 5
tc3)
coins : [2]
amo : 3
res : -1
tc4)
coins : [1, 2]
amo : 3
res : 2
tc5)
coins : [2, 3]
amo : 5
res : 2
3. Brain Storming
amo : 5
res : 2
coins : [1, 2, 3]
0 : 0
1 : 1
2 : 1
(1 + 1)
(2)
3 : 1
4 : 2
c
5 : 2 <= 1 + (4) : (4) + 1
2 + (3) : (3) + 1
3 + (2) : (2) + 1 => 2
a) dp <= [0 for _ in range(amount)]?
b) for c in coins:
for i in range(1, amoun+1) ??
for c in coins:
if i-c is possiblE
dp[i] = min(dp[i], dp[i-c]+1)
c
coins : [2, 3]
amo : 5
res : 2
i
0. 1. 2. 3. 4 5
dp : [0, i, 1, 1, 2, 2]
\
\-------dp[3] + 1
dp[2] + 1
amount 4
2 + amount(2)
'''