반갑습니다!

[프로그래머스] 예산 본문

알고리즘 문제 풀이

[프로그래머스] 예산

김덜덜이 2020. 4. 4. 17:57
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr

풀이

최대 부서의 개수를 세야하므로 최소 예산을 신청한 부서의 금액들을 budget이 넘지 않을 때까지 더해서 개수를 파악하면 된다.

코드

#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());
    int sum = 0;
    for(int i=0; i<d.size(); i++){
        if(sum + d[i] > budget) break;
        sum += d[i];
        answer++;
    }
    return answer;
}