안녕 세상아,

[c++] 구명보트 본문

프로그래머스

[c++] 구명보트

돈 많은 백수가 되고싶다 2023. 8. 17. 12:45

https://school.programmers.co.kr/learn/courses/30/lessons/42885#

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

푸는 방법은 다양할 수도 있지만 투포인터로 풀었다.

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

using namespace std;

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

    int head=0;
    int tail=people.size()-1;

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

    //투포인터로 풀기
    while(head<=tail){
        if(people[head]+people[tail]<=limit){
            head++;
            tail--;
        }
        //가장 적게 나가는 사람과 가장 많이 나가는 사람 무게 합이 limit 보다 크다면
        //가장 많이 나가는 사람은 무조건 혼자 타야함
        else
            tail--; 
        answer++;
    }
    return answer;
}