프로그래머스 java 예산

jieun·2022년 8월 19일
0

java 코테 공부

목록 보기
17/17

해결방법

  1. 최대한 많은 물품을 구매하기 위해 신청 금액이 가장 작은 물품부터 지원
  2. 예산이 0보다 작기 전까지 가격을 빼주고 개수를 카운트

전체코드

import java.util.*;
class Solution {
    public int solution(int[] d, int budget) {
        int answer = 0;
        Arrays.sort(d);
        for (int i=0; i<d.length; i++) {
            budget -= d[i]; 
            if (budget<0) break;
            answer++;
        }
        return answer;
    }
}
profile
개발새발 블로그

0개의 댓글