알고리즘/프로그래머스

Programsers 구명보트

내이름은 킹햄찌 2022. 7. 26. 23:11

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

프로그래머스 구명보트

 

아이디어

Greedy문제입니다. 한번에 두명밖에 못탄다는 점을 캐치하지 못했다면 어마어마하게 삽질 할 문제입니다.

문제를 정확하게 읽어내는 연습이 필요합니다.

 

#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 together = 0;
	//같이 탈수 있었던 사람의 수와 people에 남은 수가 같으면 보트를 다 탄거임
	while (together < people.size()) {
		//같이 탈수 있으면(2명까지만 탈 수 있음)
		if (people[together] + people.back() <= limit) {
			together++;
		}
		people.pop_back();
		answer++;

	}


	return answer;
}