Skip to content

Commit 31ed2cd

Browse files
committed
Add coin change solution
1 parent fc19c82 commit 31ed2cd

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

coin-change/hyunjung-choi.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 시간복잡도: O(amount × coins.length)
3+
* 공간복잡도: O(amount)
4+
*/
5+
6+
class Solution {
7+
fun coinChange(coins: IntArray, amount: Int): Int {
8+
val dp = IntArray(amount + 1) { Int.MAX_VALUE }
9+
dp[0] = 0
10+
11+
for (i in 1..amount) {
12+
for (coin in coins) {
13+
if (i - coin >= 0 && dp[i - coin] != Int.MAX_VALUE) {
14+
dp[i] = minOf(dp[i], dp[i - coin] + 1)
15+
}
16+
}
17+
}
18+
19+
return if (dp[amount] == Int.MAX_VALUE) -1 else dp[amount]
20+
}
21+
}

0 commit comments

Comments
 (0)