알고리즘/그리디
[C++] 프로그래머스 : 구명보트
mezo
2021. 2. 2. 00:28
728x90
반응형
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<int> people, int limit) {
int answer = 0;
int boat = 0;
sort(people.begin(), people.end());
int begin = 0;
int end = people.size()-1;
while(begin <= end){
if(begin == end){
boat++;
break;
}
if(people[begin] + people[end] <= limit){
begin++;
}
end--;
boat++;
}
answer = boat;
return answer;
}
728x90
반응형