Skip to content

Commit 0024346

Browse files
committed
add: Coin Change
1 parent 0c482e9 commit 0024346

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

coin-change/JEONGBEOMKO.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
/*
3+
time complexity: O(amount × n)
4+
space complexity: O(amount)
5+
*/
6+
public int coinChange(int[] coins, int amount) {
7+
int[] memo = new int[amount + 1];
8+
Arrays.fill(memo, amount + 1); // 초기값: 만들 수 없는 큰 수
9+
memo[0] = 0; // 0원을 만들기 위한 동전 수는 0개
10+
11+
for (int coin : coins) {
12+
for (int i = coin; i <= amount; i++) {
13+
memo[i] = Math.min(memo[i], memo[i - coin] + 1);
14+
}
15+
}
16+
17+
return memo[amount] > amount ? -1 : memo[amount];
18+
}
19+
}

0 commit comments

Comments
 (0)