문제

문제출처 : Baekjoon

img

풀이

import java.util.*;

public class Main {
    public int solution(int N, int K, int[] arr){

        int answer = 0;

        for(int i = N-1; i >= 0; i--){
            if(arr[i] <= K){
                answer += K/arr[i];
                K %= arr[i];
            }
        }

        return answer;
    }

    public static void main(String[] args) {
        Main function = new Main();
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int K = sc.nextInt();
        int[] arr = new int[N];

        for(int i = 0; i < N; i++){
            arr[i] = sc.nextInt();
        }

        System.out.println(function.solution(N, K, arr));
    }
}

회고

  1. 그리디 알고리즘으로 처음 풀어본 문제! 공부하면서 풀어본 문제는 돈이 큰값부터 들어왔을때의 계산이였고, 이건 반대로 돈이 작은 값부터 들어올때의 계산이라서 – 로 했었어야했다. 이제 조금씩 알고리즘과 관련된 문제를 풀어야지…! 하지만 많이 어렵다 ㅠㅠ 노력하자!