Skip to content

Commit cd835e1

Browse files
committed
coin change solution
1 parent 87a2701 commit cd835e1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

coin-change/Yn3-3xh.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
[문제풀이]
3+
- 배열에 주어진 수를 더해서 amount 만들기
4+
- 배열은 중복 가능
5+
- amount를 만들 수 없으면 -1
6+
- amount를 만든 동전의 최소 개수 구하기
7+
- DP
8+
time: O(N M), space: O(N)
9+
10+
[회고]
11+
DP로 풀면 되지 않을까 라는 짐작은 가는데,
12+
아직 풀이방법이 부족하다..
13+
해결방법을 보고 겨우 이해는 했다..
14+
*/
15+
class Solution {
16+
public int coinChange(int[] coins, int amount) {
17+
int[] dp = new int[amount + 1];
18+
Arrays.fill(dp, amount + 1);
19+
dp[0] = 0;
20+
21+
for (int coin : coins) {
22+
for (int i = coin; i <= amount; i++) {
23+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
24+
}
25+
}
26+
return dp[amount] > amount ? -1 : dp[amount];
27+
}
28+
}
29+

0 commit comments

Comments
 (0)