File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ λ¬Έμ : μ£Όμ΄μ§ λμ μ’
λ₯λ‘ νΉμ κΈμ‘μ λ§λ€κΈ° μν μ΅μ λμ κ°μλ₯Ό ꡬνμμ€.
3+ νμ΄: λμ κ³νλ²(DP)μ μ¬μ©νμ¬ κ° κΈμ‘μ λν΄ μ΅μ λμ κ°μλ₯Ό κ³μ°ν©λλ€. λ§μ½ νΉμ κΈμ‘μ λ§λ€ μ μλ€λ©΄ -1μ λ°νν©λλ€.
4+ μκ° λ³΅μ‘λ: O(n * m), nμ κΈμ‘(amount), mμ λμ μ’
λ₯μ κ°μμ
λλ€. κ° κΈμ‘μ λν΄ λͺ¨λ λμ μ νμΈνλ―λ‘ μ 체 μκ° λ³΅μ‘λλ O(n * m)μ
λλ€.
5+ κ³΅κ° λ³΅μ‘λ: O(n), κΈμ‘(amount)κΉμ§μ μ΅μ λμ κ°μλ₯Ό μ μ₯νλ DP λ°°μ΄μ μ¬μ©νλ―λ‘ κ³΅κ° λ³΅μ‘λλ O(n)μ
λλ€.
6+ μ¬μ©ν μλ£κ΅¬μ‘°: λ°°μ΄(DP λ°°μ΄)
7+ '''
8+
9+
10+ class Solution :
11+ def coinChange (self , coins : List [int ], amount : int ) -> int :
12+ if amount == 0 :
13+ return 0
14+ if len (coins ) == 1 and coins [0 ] > amount :
15+ return - 1
16+ if amount in coins :
17+ return 1
18+
19+ dp = [0 for i in range (amount + 1 )]
20+
21+ for i in range (amount + 1 ):
22+ for j in range (len (coins )):
23+ if i == 0 and coins [j ] < amount :
24+ dp [coins [j ]] = 1
25+ elif i > 0 and 0 <= i + coins [j ] <= amount :
26+ if dp [i ] > 0 and dp [i + coins [j ]] > 0 :
27+ dp [i + coins [j ]] = min (dp [i + coins [j ]], dp [i ]+ 1 )
28+ elif dp [i ] > 0 and dp [i + coins [j ]] == 0 :
29+ dp [i + coins [j ]] = dp [i ]+ 1
30+
31+
32+
33+
34+ if dp [amount ] == 0 :
35+ return - 1
36+ return dp [amount ]
37+
38+
You canβt perform that action at this time.
0 commit comments