그리디 알고리즘으로 쉽게 해결된다.
package BAEKJOON;
import java.util.Scanner;
public class Coin0_11047 {
static int[] coins;
public static int getCoinCount(int k) {
int r = 0;
// 이분탐색 써서 시작점 잡아도 좋겠다.
for (int i = coins.length - 1; i >= 0; i--) {
if (coins[i] > k)
continue;
int c = k / coins[i];
k -= coins[i] * c;
r += c;
if (k == 0)
break;
}
return r;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
coins = new int[n];
int k = sc.nextInt();
for (int i = 0; i < n; i++) {
coins[i] = sc.nextInt();
}
sc.close();
System.out.println(getCoinCount(k));
}
}
'알고리즘' 카테고리의 다른 글
백준 회의실배정-1931 (0) | 2020.11.08 |
---|---|
백준 미로탐색-2178 (0) | 2020.11.08 |
백준 DFSBFS-1260 (0) | 2020.11.08 |
백준 종이의 개수-1780 (0) | 2020.11.08 |
백준 단지번호붙히기-2667 (0) | 2020.11.08 |