File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class Solution :
2+ def coinChange (self , coins : list [int ], amount : int ) -> int :
3+ """amount๋ฅผ ๋ง๋ค๊ธฐ ์ํด ์ต์ํ์ ์ฝ์ธ์ผ๋ก ๊ตฌ์ฑํ ์ ์๋ ๋ฐฉ์์ ์๋ฅผ ๊ตฌํ๋ ํจ์
4+ ๋์ผํ ๋์ ์ ์ฌ๋ฌ๋ฒ ์ฌ์ฉํด๋ ๋จ
5+
6+ ๋ฐฉ๋ฒ:
7+ - ๋ฐฉ๋ฒ ์๋ฅผ ๋ณด๊ณ dp๋ก ํ๊ธฐ๋ก ํจ. ๊ธ์ก์ด dp ๋จ๊ณ๊ฐ ๋จ.
8+ ๋ชจ๋ ๊ธ์ก๋ณ๋ก, ์ฝ์ธ ๋ชฉ๋ก์ ๋๋ฉด์, ์ฝ์ธ์ ์ฌ์ฉํ ์ ์์ผ๋ฉด amount-coinํ ๊ธ์ก์ ๋ฐฉ๋ฒ์ + 1๊ณผ ๋น๊ตํ๊ธฐ
9+ -> ์กฐํฉ์ด ๋ถ๊ฐ๋ฅํ ์ํ๋ฅผ ์ด๋ป๊ฒ ํ๋จํ์ง? ์์ dp init์ 0์ด ์๋๋ผ inf๋ก ํด์ผ, ๊ตฌ๋ถ์ด ๊ฐ๋ฅํ๋ค๋ ์ .
10+ -> ๊ทธ๋์ min ๋น๊ต๋ฅผ ํ์ฌ ์ธ๋ฑ์ค dp๊ฐ๊ณผ i-coin์ dp ๊ฐ + 1์ผ๋ก ํจ
11+ coin์ด ์ ๋ ฌ๋์ง ์์ ๋ฆฌ์คํธ๋ผ, ์ ๋ ฌํด, coin ๊ฐ์ด amount๋ฅผ ๋์ด๊ฐ๋ฉด pass
12+
13+ Args:
14+ coins (list[int]): ์ฌ์ฉํ ์ ์๋ ๋์ ๋ชฉ๋ก
15+ amount (int): ๋ชฉํํ๋ ๊ธ์ก
16+
17+ Returns:
18+ int: ๊ธ์ก์ ๋ง๋ค๊ธฐ ์ํด ๊ตฌ์ฑํ ์ ์๋ ๋์ ๋ค ๋ฐฉ์ ์
19+ """
20+ dp = [float ("inf" )] * (amount + 1 )
21+ dp [0 ] = 0
22+ coins .sort ()
23+ for coin in coins :
24+ if coin > amount :
25+ continue
26+ for i in range (coin , amount + 1 ):
27+ dp [i ] = min (dp [i ], 1 + dp [i - coin ])
28+ return dp [amount ] if dp [amount ] != float ("inf" ) else - 1
You canโt perform that action at this time.
0 commit comments