슈콩

[BOJ] 백준 11047 동전 0 본문

Algorithms/Baekjoon

[BOJ] 백준 11047 동전 0

shukong 2025. 8. 27. 13:20

[문제]

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

 

 

[소스 코드]

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,100000001);
    	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]==100000001) {
    		System.out.println(-1);
    		return;
    	}
    	System.out.println(dp[k]);
    }
}

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

[BOJ] 백준 1026 보물  (0) 2025.08.27
[BOJ] 백준 1931 회의실 배정  (2) 2025.08.27
[BOJ] 백준 9657 돌 게임 3  (4) 2025.08.27
[BOJ] 백준 9655 돌게임  (0) 2025.08.26
[BOJ] 백준 9465 스티커  (0) 2025.08.26