구명보트

bgy·2022년 10월 12일
0

투포인터로 품
제일 가벼운 사람이랑 제일 무거운 사람 합이 한계보다 크면 무거운 사람만 구출
그렇지 않으면 함께 구출

#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 p1=0;
    int p2=people.size()-1;
    while(p1<p2){
        if(people[p1]+people[p2]>limit){
            answer++;
            p2--;
        }
        else{
            answer++;
            p1++;
            p2--;
        }
    }
    if(p1==p2)
        answer++;
    return answer;
}

0개의 댓글