problem solving
[프로그래머스]구명보트
Shinuk Yi
2020. 3. 11. 08:20
정렬을 한 후 가장작은 값의 위치(front)와 가장 큰 값의 위치(end)의 값을 더한 값이 limit보다 크다면
한 명밖에 못타기 때문에 end를 한칸 줄여 가장 큰 값을 하나 제거한다.
반대로 limit보다 작다면 두 명이 탈 수 있기에 front++, end--를한다.
#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 front = 0;
int end = people.size() - 1;
while(front <= end) {
if (people[front] + people[end] > limit) {
end--;
answer++;
}
else if (people[front] + people[end] <= limit) {
front++, end--;
answer++;
}
}
return answer;
}
|
cs |