Publish:

태그: ,

카테고리:

https://school.programmers.co.kr/learn/courses/30/lessons/42885
난이도 ⭐⭐

문제

42626


나의 풀이

나는 무거운 사람부터 보트를 태워 보내는걸로 생각했다.
그래서 먼저 sort를 사용하여 오름차순으로 벡터를 정렬해줬다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

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

    int start = 0;
    int end = people.size() - 1;

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

    while (start <= end)
    {
        // 최소 몸무게 + 최대 몸무게 값이 limit 을 넘으면, 최대 몸무게는 혼자 타야 함 (보트 하나 ㅃㅃ answer++)

        if (people[start] + people[end] <= limit)
        {
            // 같이 탈 수 있다는 뜻이니 다음 최소 몸무게 오세요
            start++;
        }
        
        end--;  // 최대 몸무게 사람 ㅃㅃ, 그다음 최대 몸무게 오세요
        answer++; // 보트 태워 보냄... 보트 ㅃㅃ
    }

    return answer;
}

이슈 및 공부한 것을 기록해두는 개인 블로그 입니다. 댓글, 피드백 환영합니다 🙂

Update:

댓글남기기