Skip to content

Commit b13a229

Browse files
committed
Solved "Coin change"
1 parent 554a5ed commit b13a229

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

โ€Žcoin-change/KwonNayeon.pyโ€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Constraints:
3+
1. 1 <= coins.length <= 12
4+
2. 1 <= coins[i] <= 2^31 - 1
5+
3. 0 <= amount <= 10^4
6+
7+
Time Complexity: O(N*M)
8+
- N์€ amount
9+
- M์€ ๋™์ „์˜ ๊ฐœ์ˆ˜ (coins.length)
10+
11+
Space Complexity: O(N)
12+
- N์€ amount
13+
14+
To Do:
15+
- DP ๋ฌธ์ œ ๋ณต์Šตํ•˜๊ธฐ
16+
- Bottom-up๊ณผ Top-down ๋ฐฉ์‹์˜ ์ฐจ์ด์  ์ดํ•ดํ•˜๊ธฐ
17+
- ๊ทธ๋ฆฌ๋””์™€ DP์˜ ์ฐจ์ด์  ๋ณต์Šตํ•˜๊ธฐ
18+
"""
19+
20+
class Solution:
21+
def coinChange(self, coins: List[int], amount: int) -> int:
22+
dp = [sys.maxsize // 2] * (amount + 1)
23+
dp[0] = 0
24+
25+
for i in range(1, amount + 1):
26+
for coin in coins:
27+
if i >= coin:
28+
dp[i] = min(dp[i], dp[i-coin] + 1)
29+
30+
return dp[amount] if dp[amount] != sys.maxsize // 2 else -1

0 commit comments

Comments
ย (0)