We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c159fc1 commit 65e96d0Copy full SHA for 65e96d0
1 file changed
βcoin-change/gcount85.pyβ
@@ -0,0 +1,26 @@
1
+"""
2
+# Intuition
3
+dp[n]μ nμμ λ§λλλ° νμν μ΅μ λμ κ°μ
4
+e.g. n = 11μΌ λ, 11μμ λ§λ€κΈ° μν μ΅μ λμ κ°μλ 11μμ coinsμ μμλ₯Ό λΊ dp κ° + 1μ΄λ€.
5
+
6
+# Complexity
7
+- Time complexity: O(amount * coins.length)μΈλ°, coins λ°°μ΄ κΈΈμ΄κ° μμλΌμ 무μ => O(amount)
8
9
+- Space complexity: dp λ°°μ΄ μμ±μΌλ‘ O(amount)
10
11
12
13
+class Solution:
14
+ def coinChange(self, coins: list[int], amount: int) -> int:
15
+ coins.sort()
16
+ INF = float("inf")
17
+ dp = [INF] * (amount + 1)
18
+ dp[0] = 0
19
+ for i in range(amount + 1):
20
+ if dp[i] == INF:
21
+ continue
22
+ for c in coins:
23
+ if i + c > amount:
24
+ break
25
+ dp[i + c] = min(dp[i + c], dp[i] + 1)
26
+ return dp[-1] if dp[-1] != INF else -1
0 commit comments