슈콩

[BOJ] 백준 2294 동전 2 본문

Algorithms/Baekjoon

[BOJ] 백준 2294 동전 2

shukong 2025. 8. 26. 13:18

[문제]

https://www.acmicpc.net/problem/2294

 

 

[소스 코드]

import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer st = new StringTokenizer(br.readLine());
    	int n= Integer.parseInt(st.nextToken());
    	int k = Integer.parseInt(st.nextToken());
    	int[] coin = new int[n+1];
    	for(int i=1;i<=n;i++) {
    		coin[i] = Integer.parseInt(br.readLine());
    	}
    	int[] dp = new int[k+1];
    	Arrays.fill(dp, 10001);
    	dp[0] = 0;
    	for(int i=1;i<=n;i++) {
    		for(int j=coin[i];j<=k;j++) {
    			dp[j] = Math.min(dp[j], dp[j-coin[i]] + 1);
    		}
    	}
    	if(dp[k]==10001)
    		System.out.println(-1);
    	else
    		System.out.println(dp[k]);
    }
}

 

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 2482 색상환  (0) 2025.08.26
[BOJ] 백준 2302 극장 좌석  (2) 2025.08.26
[BOJ] 백준 2293 동전 1  (0) 2025.08.26
[BOJ] 백준 2240 자두 나무  (0) 2025.08.25
[BOJ] 백준 2193 이친수  (0) 2025.08.25