study
구명보트[프로그래머스]
Date: 2026-01-28 06:59
Update: 2026-01-28 07:01


[프로그래머스] 구명보트

접근법:

  1. 구명 보트에 최대 2명이 탈 수 있고 구명보트의 최대치는 항상 가장 무거운 사람보다 많다.
  2. 주어진 배열을 정렬하고 가장 무거운사람을 무조건 태우고 만약 무게가 남는 다면 현재 남아있는 가장 가벼운 사람을 태운다.
#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 startIndex = 0;
    int endIndex = people.size() - 1;
    
    while(startIndex <= endIndex)
    {
        if(people[startIndex] + people[endIndex] <= limit)
            startIndex++;
        endIndex--;
        answer++;
    }
    return answer;
}