-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLv2_구명보트.cpp
56 lines (51 loc) · 1023 Bytes
/
Lv2_구명보트.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int solution(vector<int> people, int limit) {
int answer = 0;
int index = 0;
sort(people.begin(), people.end());
while (people.size()-index !=0) {
int end = people.size() - 1;
if (end == index) { //한 명 남았을 때
answer++;
break;
}
else {
int sum = people[index] + people[end];
bool check = true;
while (sum > limit) {
people.pop_back();
answer++;
end = people.size() - 1;
if (end == index) {
check = false;
break;
}
sum = people[index] + people[end];
}
answer++;
if (check)
people.pop_back();
index++;
}
}
return answer;
}
/*
int solution(vector<int> people, int limit) {
int answer = 0;
int size = people.size();
sort(people.begin(), people.end());
reverse(people.begin(), people.end());
for (int i = 0, j = size - 1; i <= j; i++) {
if (people[i] + people[j] <= limit) {
j--;
}
answer++;
}
return answer;
}
*/