https://level.goorm.io/exam/195697/%EA%B3%BC%EC%9D%BC-%EA%B5%AC%EB%A7%A4/quiz/1
import java.util.*;
class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); //마트에서 파는 과일의 개수
int money = sc.nextInt(); //플레이어가 가진 돈
int[][] fruits = new int[N][3];
for(int i=0;i<N;i++){
fruits[i][0] = sc.nextInt(); //과일의 가격
fruits[i][1] = sc.nextInt(); //과일의 포만감
fruits[i][2] = fruits[i][1]/fruits[i][0]; //조각의 포만감
}
Arrays.sort(fruits, new Comparator<int[]>(){
@Override
public int compare(int[] f1, int[] f2){
return f2[2] - f1[2];
}
});
Long satiety = 0L;
for(int i=0;i<N;i++){
if(money == 0){
break;
}
if(money>=fruits[i][0]){
money -= fruits[i][0];
satiety += fruits[i][1];
}else{
satiety += fruits[i][2]*money;
money = 0;
}
}
System.out.println(satiety);
}
}
그리드를 사용하면 되는 문제였다. 과일의 조각의 포만감으로 정렬을 해서 한 조각당 포만감이 높은 순서의 과일부터 차례로 구매하도록 하였다. 이 문제를 풀면서 하나 주의해야할 점은 테스트케이스 2번이 INT형의 범위를 넘어가기 때문에 포만감의 경우 Long형으로 해야한다.
'JAVA' 카테고리의 다른 글
[프로그래머스/자바] A로 B 만들기 (0) | 2023.09.03 |
---|---|
[프로그래머스/자바] 다항식 더하기 (0) | 2023.09.01 |
[프로그래머스/자바] 자릿수 더하기 (0) | 2023.08.31 |
구름톤 챌린지 13일차 - 발전기(2) (0) | 2023.08.30 |
구름톤 챌린지 12일차 - 발전기 (0) | 2023.08.29 |
댓글