프로그래머스 구명보트

조항주·2022년 4월 19일
0

algorithm

목록 보기
22/50
post-thumbnail

문제

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

코드

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

using namespace std;

bool compare(int a,int b){return a>b;}
int solution(vector<int> people, int limit) {
    int answer=0;
    sort(people.begin(), people.end(),compare);
   
    for(int i=0;i<people.size();i++){
        if(people[i]+people[people.size()-1]<=limit){
            people.pop_back();
        }
        answer++;
    }
    return answer;
}

풀이

무인도에 갇힌 사람들을 2인승 구명보트를 통해서 구해주는데 모든 사람들을 구하는 최소의 구명보트 갯수를 구하면 됩니다. 구명보트의 무게제한과 갇힌 사람들의 무게를 입력으로 줍니다.

저는 무게가 제일 많이 나가는 순으로 사람들을 정렬 한 후에 구명보트에 앞사람부터 태우고 만약 제일 가벼운 사람을 태워도 무게가 초과되지 않는다면 같이 태우면서 카운팅 했습니다.

0개의 댓글