본문 바로가기
알고리즘/프로그래머스

알고리즘(C++) / 프로그래머스 level 2 : 구명보트

by clean_h 2021. 7. 14.
728x90

level 2 : 구명보트

https://programmers.co.kr/learn/courses/30/lessons/42885?language=cpp 

 

코딩테스트 연습 - 구명보트

무인도에 갇힌 사람들을 구명보트를 이용하여 구출하려고 합니다. 구명보트는 작아서 한 번에 최대 2명씩 밖에 탈 수 없고, 무게 제한도 있습니다. 예를 들어, 사람들의 몸무게가 [70kg, 50kg, 80kg, 5

programmers.co.kr

 

코드

//프로그래머스
#include <iostream>
#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 big = people.size() - 1;
    int small = 0;

    while (small <= big) {
        if (people[small] + people[big] <= limit) {
            small++;
        }
        big--;
        answer++;
    }

    return answer;
}

int main() {
    vector<int> people = { 70, 80,50 };
    int limit = 100;
    cout << solution(people, limit) << "\n";
    return 0;
}

 

고찰

한 보드에 두 명까지 탈 수 있다는 조건을 보지못하고,,, 구현해서 틀렸다.

문제를 꼼꼼하게 읽고,,, 조건을 파악하도록 하자.

 

728x90

댓글