[프로그래머스/C++] 예산

꿈별·2024년 1월 16일
0

문제풀이

목록 보기
38/52

문제


풀이

#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<int> d, int budget) {
    int answer = 0;
    // 예산 안에서 최대한 많은 "부서" count개의 물품 구매하기
    // 따라서 오름차순 정렬, 신청 금액이 적은 부서부터 지원
    sort(d.begin(), d.end());
    for (int i = 0; i < d.size(); i++)
    {
        if (0 <= (budget -= d[i])) {
            answer++;
        }
    }
    return answer;
}

sort()

https://learn.microsoft.com/ko-kr/cpp/standard-library/algorithm-functions?view=msvc-170#sort
https://zoosso.tistory.com/1086

지정된 범위에 있는 요소를 비내림차순 또는 이진 조건자로 지정한 정렬 기준에 따라 정렬한다

  • 헤더
#include <algorithm>
  • 원형
void sort(T first, T last);

void sort(T first, T last, Compare pred);

0개의 댓글