ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [LV1] 예산
    PS/프로그래머스 2022. 10. 2. 15:19

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

    using namespace std;
    int min_value;
    int min_index;


    int solution(vector<int> d, int budget) {

    int answer = 0;
    int cur_value = 0; 

    while (d.size()>0) // is_reamin test : 
    {
    min_value = *min_element(d.begin(), d.end());
    min_index = min_element(d.begin(), d.end())-d.begin();

    if (cur_value + min_value > budget) 
    {
    break;
    }
    else  
    {
    cur_value += min_value;
    answer++;
    d.erase(d.begin() + min_index);
    }
    }
    return answer;
    }

     

     

    프로그래머스 lv1 예산 문제.

    가장 작은 것만 찾아서 넣으면 그게 제일 큰 answer 값 뱉을 확률이 있음.

     

    풀다 segmentation fault (core dump) 

Designed by Tistory.