Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Study

프로그래머스-예산 본문

알고리즘/C++ 문제풀이

프로그래머스-예산

^_^? 2021. 7. 5. 14:01
 

코딩테스트 연습 - 예산

S사에서는 각 부서에 필요한 물품을 지원해 주기 위해 부서별로 물품을 구매하는데 필요한 금액을 조사했습니다. 그러나, 전체 예산이 정해져 있기 때문에 모든 부서의 물품을 구매해 줄 수는

programmers.co.kr

 

 

<풀이>

더보기

#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int solution(vector<int> d, int budget) {
     int answer = 0;
 
    sort(d.begin(), d.end());
 
    for (int i = 0; i < d.size(); i++)
    {
        if (budget - d[i] < 0)
            break;
        answer++;
        budget -= d[i];
    }
    
    return answer;
}

 

 

-참고한 사이트

https://hellominchan.tistory.com/281