Skip to content

Commit 9c7b41a

Browse files
authored
Merge pull request #1029 from AlgorithmWithGod/zinnnn37
[20251003] BOJ / G5 / 동전 2 / 김민진
2 parents b40ad45 + 098ed42 commit 9c7b41a

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
```java
2+
package etc;
3+
4+
import java.io.*;
5+
import java.util.Arrays;
6+
import java.util.StringTokenizer;
7+
8+
public class BJ_2294_동전_2 {
9+
10+
private static final int INF = 987654321;
11+
12+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
14+
private static StringTokenizer st;
15+
16+
private static int N, K;
17+
private static int[] coins, dp;
18+
19+
public static void main(String[] args) throws IOException {
20+
init();
21+
sol();
22+
}
23+
24+
private static void init() throws IOException {
25+
st = new StringTokenizer(br.readLine());
26+
N = Integer.parseInt(st.nextToken());
27+
K = Integer.parseInt(st.nextToken());
28+
29+
coins = new int[N + 1];
30+
for (int i = 1; i <= N; i++) {
31+
coins[i] = Integer.parseInt(br.readLine());
32+
}
33+
34+
dp = new int[K + 1];
35+
Arrays.fill(dp, INF);
36+
dp[0] = 0;
37+
}
38+
39+
private static void sol() throws IOException {
40+
for (int i = 1; i <= K; i++) {
41+
for (int coin : coins) {
42+
if (i - coin >= 0 && dp[i - coin] != INF) {
43+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
44+
}
45+
}
46+
}
47+
bw.write(dp[K] != INF ? (dp[K] + "") : "-1");
48+
bw.flush();
49+
bw.close();
50+
br.close();
51+
}
52+
53+
}
54+
```

0 commit comments

Comments
 (0)