코딩 테스트

프로그래머스 - 구명보트

__sapar 2024. 8. 23. 13:21
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<int> people, int limit) {
    int answer = 0;
    
    int cachelimit = limit;
    
    sort(people.begin(), people.end());
    
    int min = 0;
    
    int max = people.size()-1;
    
    while(min <= max)
    {
        if((people[min] + people[max])<= cachelimit)
        {
            answer++;
            min++;
            max--;
        }
        else
        {
            answer++;
            max--;
        }        
    }  
    
    
    return answer;
}