Skip to content

Commit 8203cf9

Browse files
authored
Merge pull request #803 from AlgorithmWithGod/lkhyun
[20250902] BOJ / G5 / 동전 2 / 이강현
2 parents 2cc33f7 + de390e7 commit 8203cf9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static int N,K;
10+
static Set<Integer> coins;
11+
static int[] dp; //dp[i]: i원을 만드는데 필요한 동전의 최소 개수
12+
public static void main(String[] args) throws IOException {
13+
st = new StringTokenizer(br.readLine());
14+
N = Integer.parseInt(st.nextToken());
15+
K = Integer.parseInt(st.nextToken());
16+
coins = new HashSet<>();
17+
dp = new int[K+1];
18+
for (int i = 0; i < N; i++) {
19+
coins.add(Integer.parseInt(br.readLine()));
20+
}
21+
22+
Arrays.fill(dp,Integer.MAX_VALUE);
23+
dp[0] = 0;
24+
25+
for (int i : coins) {
26+
for (int j = i; j <= K; j++) {
27+
if(dp[j-i] != Integer.MAX_VALUE){
28+
dp[j] = Math.min(dp[j],dp[j-i]+1);
29+
}
30+
}
31+
}
32+
int ans = dp[K] == Integer.MAX_VALUE ? -1 : dp[K];
33+
34+
bw.write(ans + "");
35+
bw.close();
36+
}
37+
}
38+
```

0 commit comments

Comments
 (0)