File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Solution :
5+ def coinChange (self , coins : List [int ], amount : int ) -> int :
6+ # dp[i]: i ๊ธ์ก์ ๋ง๋ค๊ธฐ ์ํด ํ์ํ ์ต์ ๋์ ๊ฐ์
7+ dp = [float ('inf' )] * (amount + 1 )
8+ dp [0 ] = 0
9+
10+ for i in range (1 , amount + 1 ):
11+ for coin in coins :
12+ if coin <= i :
13+ dp [i ] = min (dp [i ], dp [i - coin ] + 1 )
14+
15+ return dp [amount ] if dp [amount ] != float ('inf' ) else - 1
16+
17+
18+ # ์๊ฐ ๋ณต์ก๋:
19+ # - ์ธ๋ถ ๋ฐ๋ณต๋ฌธ์ ๊ธ์ก(amount)์ ๋ฒ์์ ๋น๋กํ๊ณ -> O(n) (n์ amount)
20+ # - ๋ด๋ถ ๋ฐ๋ณต๋ฌธ์ ๋์ ์ ๊ฐ์์ ๋น๋กํ๋ฏ๋ก -> O(m) (m์ coins์ ๊ธธ์ด)
21+ # - ์ด ์๊ฐ ๋ณต์ก๋: O(n * m)
22+
23+ # ๊ณต๊ฐ ๋ณต์ก๋:
24+ # - dp ๋ฐฐ์ด์ ๊ธ์ก(amount)์ ํฌ๊ธฐ๋งํผ์ ๊ณต๊ฐ์ ์ฌ์ฉํ๋ฏ๋ก O(n)
25+ # - ์ถ๊ฐ ๊ณต๊ฐ ์ฌ์ฉ์ ์์ผ๋ฏ๋ก ์ด ๊ณต๊ฐ ๋ณต์ก๋: O(n)
You canโt perform that action at this time.
0 commit comments