Post

[Summer/Winter Coding(~2018)] 예산 (C++)

https://school.programmers.co.kr/learn/courses/30/lessons/12982

풀이

주어진 d를 sort해준 후, 작은 수부터 더해가며 예산을 초과하면 반복문을 빠져나가는 방식으로 풀 수 있었다.

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<int> d, int budget) {
    sort(d.begin(), d.end());
    
    int sum = 0, cnt = 0;
    for(auto cur : d){
        sum += cur;
        if(sum > budget)   break;
        cnt++;
    }
    return cnt;
}
This post is licensed under CC BY 4.0 by the author.