Algorithm Solving/Java

[BOJ] 백준 11047번 : 동전 - Java

기만나🐸 2024. 9. 9. 21:25

현재 차례의 최고의 답을 찾는 문제

  • 다른 금액의 동전이 여러 개 주어졌을 때 M원을 만드는 최소의 개수

 

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

import java.io.*;
import java.util.*;

public class Main {
	static int N, K;
	static int[] arr;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        N = Integer.parseInt(st.nextToken());
        K = Integer.parseInt(st.nextToken());
        arr = new int[N];
        for (int i=0; i<N; i++) {
        	arr[i] = Integer.parseInt(br.readLine());
        }
        
        int cnt = 0;
        int index = arr.length - 1;
        while (true) {
        	if (K == 0) break;
        	
        	if (arr[index] <= K) {
        		K -= arr[index];
        		cnt ++;
        	}
        	else {
        		index --;
        	}
        }
        
        System.out.println(cnt);
    }
}