Recent Posts
«   2024/12   »
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
Today
Total
관리 메뉴

Try

[Programmers/C++] 구명보트 본문

Algorithm/Programmers

[Programmers/C++] 구명보트

HAS3ONG 2019. 3. 23. 21:35

출처

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



소스

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
35
#include <vector>
#include <algorithm>
 
using namespace std;
 
int solution(vector<int> p, int limit) {
    int answer = 0;
    int boat = 0;
    int i = 0;
    int j = p.size()-1;
    
    sort(p.begin(), p.end());
    reverse(p.begin(), p.end());
    
    while(1){
        if(i > j){
            break;
        }
        else if(i == j){
            boat++;
            break;
        }
        if(p[i] + p[j] > limit){
            i++;
            boat++;
        }
        else{
            i++;
            j--;
            boat++;
        }
    }
    answer = boat;
    return answer;
}
cs


Comments