From de390e756f014853655928947282e8cf0893b551 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Tue, 2 Sep 2025 23:35:28 +0900 Subject: [PATCH] =?UTF-8?q?[20250902]=20BOJ=20/=20G5=20/=20=EB=8F=99?= =?UTF-8?q?=EC=A0=84=202=20/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../02 BOJ G5 \353\217\231\354\240\204 2.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 "lkhyun/202509/02 BOJ G5 \353\217\231\354\240\204 2.md" diff --git "a/lkhyun/202509/02 BOJ G5 \353\217\231\354\240\204 2.md" "b/lkhyun/202509/02 BOJ G5 \353\217\231\354\240\204 2.md" new file mode 100644 index 00000000..0621842d --- /dev/null +++ "b/lkhyun/202509/02 BOJ G5 \353\217\231\354\240\204 2.md" @@ -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 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(); + } +} +```