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 824a904 commit 11581cbCopy full SHA for 11581cb
βcoin-change/gmlwls96.ktβ
@@ -0,0 +1,25 @@
1
+class Solution {
2
+ // μκ³ λ¦¬μ¦ : dp
3
+ /** νμ΄
4
+ * dpλ°°μ΄μ μ΅μνμ λμ μ κ°μλ₯Ό μ μ₯.
5
+ * dp[i] = min(dp[i - λμ κ°], dp[i]) μ€ λ μμκ°μ΄ μ΅μ λμ μ κ°μ.
6
+ * */
7
+ // μκ° : O(coins.len*amount), κ³΅κ° : O(amount)
8
+ fun coinChange(coins: IntArray, amount: Int): Int {
9
+ val initValue = Int.MAX_VALUE / 2
10
+ val dp = IntArray(amount + 1) { initValue }
11
+ dp[0] = 0
12
+ for (i in 1..amount) {
13
+ coins.forEach { c ->
14
+ if (c <= i) {
15
+ dp[i] = min(dp[i - c] + 1, dp[i])
16
+ }
17
18
19
+ return if (dp[amount] == initValue) {
20
+ -1
21
+ } else {
22
+ dp[amount]
23
24
25
+}
0 commit comments