매일 Algorithm

신재원·2023년 1월 15일
0

Algorithm

목록 보기
7/243

프로그래머스 : 예산 (LEVEL 1)

import java.util.Arrays;

class Solution {
    public int solution(int[] d, int budget) {
        Arrays.sort(d);
        int answer = 0;
        
        for (int i = 0; i < d.length; i++) {
            budget -= d[i];
          /*  d	        budget	result
             [2,2,3,3]	  10	     4

             배열을 계속 빼다보면 budget이 0이 나오는데 
             그경우는 ++가 적용되지 않음으로
             budget이 0보다 작으면 break; 해준다.

          */
            if (budget < 0) {
                break;
            } else {
                answer++;
            }
        }
        return answer;
    }
}

0개의 댓글