관리 메뉴

Partially Committed

[Summer/Winter Coding(~2018)] μ˜ˆμ‚° λ³Έλ¬Έ

πŸ”₯ Algorithm || λ¬Έμ œν’€μ΄/PS

[Summer/Winter Coding(~2018)] μ˜ˆμ‚°

WonderJay 2022. 7. 4. 18:27
728x90
λ°˜μ‘ν˜•
SMALL

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

 

μ½”λ”©ν…ŒμŠ€νŠΈ μ—°μŠ΅ - μ˜ˆμ‚°

Sμ‚¬μ—μ„œλŠ” 각 λΆ€μ„œμ— ν•„μš”ν•œ λ¬Όν’ˆμ„ 지원해 μ£ΌκΈ° μœ„ν•΄ λΆ€μ„œλ³„λ‘œ λ¬Όν’ˆμ„ κ΅¬λ§€ν•˜λŠ”λ° ν•„μš”ν•œ κΈˆμ•‘μ„ μ‘°μ‚¬ν–ˆμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜, 전체 μ˜ˆμ‚°μ΄ μ •ν•΄μ Έ 있기 λ•Œλ¬Έμ— λͺ¨λ“  λΆ€μ„œμ˜ λ¬Όν’ˆμ„ ꡬ맀해 쀄 μˆ˜λŠ”

programmers.co.kr

주어진 μ˜ˆμ‚°μœΌλ‘œ μ΅œλŒ€ν•œ λ§Žμ€ λΆ€μ„œλ₯Ό μ§€μ›ν•΄μ•Όν•œλ‹€.

사싀 κ°„λ‹¨ν•˜λ‹€.

λΆ€μ„œ 별 μš”μ²­ μ˜ˆμ‚°μ„ 담은 배열인 d λ₯Ό μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œ λ’€,

budget μ—μ„œ λ°°μ—΄ d 의 μ›μ†Œλ₯Ό μ°¨λ‘€λŒ€λ‘œ 뺐을 λ•Œ,

μ–Όλ§ˆλ‚˜ λ§Žμ€ μ›μ†Œλ₯Ό λΊ„ 수 μžˆλŠ”μ§€λ₯Ό κ΅¬ν•˜λ©΄ λœλ‹€.

 

#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 (auto& ele : d)
    {
        if (budget - ele < 0) break;
        budget -= ele;
        answer++;
    }
    
    return answer;
}

 

 

 

728x90
λ°˜μ‘ν˜•
LIST
Comments