프로그래머스 예산 C++

이선아·2022년 2월 6일
2

📌 vector 안의 값 정렬하기! sort(); 사용
vector<int> d 로 주어졌으니까 sort(d.begin(), d.end()); 벡터 맨 앞부터 맨 뒤까지 정렬해준다는 의미 #include <algorithm> 포함


Programmers 예산.cpp

#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

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

정렬된 벡터 d를 sum에 순서대로 집어넣으면서 sum이 예산(budget)과 적거나 같을 때까지만 count를 하나 올려주어 리턴해준다.

profile
깃허브 놀러오세용 -> Tistory로 블로그 이전합니다.

0개의 댓글