Coding_Test 연습/Programmers

[프로그래머스] (C++) LV2 구명보트

Codetesing 2022. 6. 1. 13:58

크게 어려운건 없었다.

투포인터 방식을 이용해서 풀었다.

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<int> people, int limit) {
    int answer = 0;

    sort(people.begin(), people.end());

    int start = 0, end = people.size() - 1;

    while (start <= end)
    {
        if (people[start] + people[end] <= limit)
        {
            start++;
            end--;
            answer++;
        }
        else
        {
            end--;
            answer++;
        }
    }

    return answer;
}