Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lkhyun/202509/02 BOJ G5 동전 2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
```java
import java.io.*;
import java.util.*;

public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st;
static int N,K;
static Set<Integer> coins;
static int[] dp; //dp[i]: i원을 만드는데 필요한 동전의 최소 개수
public static void main(String[] args) throws IOException {
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
coins = new HashSet<>();
dp = new int[K+1];
for (int i = 0; i < N; i++) {
coins.add(Integer.parseInt(br.readLine()));
}

Arrays.fill(dp,Integer.MAX_VALUE);
dp[0] = 0;

for (int i : coins) {
for (int j = i; j <= K; j++) {
if(dp[j-i] != Integer.MAX_VALUE){
dp[j] = Math.min(dp[j],dp[j-i]+1);
}
}
}
int ans = dp[K] == Integer.MAX_VALUE ? -1 : dp[K];

bw.write(ans + "");
bw.close();
}
}
```