Programmers/Level 2

[프로그래머스 2] 구명 보트 (C/C++) (★)

워니- 2019. 10. 23. 15:27
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
33
34
#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 i = 0;
    for(int j = people.size()-1; j >= 0; j--)
    {
        // 더 이상 태울 사람이 없는 경우 종료
        if(i > j)
        {
            break;
        }
        
        // 첫번째와 마지막 사람의 무게 합
        if(people[i] + people[j] <= limit)
        {
            i++;
            answer++;
        }
        else
        {
            answer++;
        }
    }
    
    return answer;
}
cs