문제 링크

요약

  • 문제를 잘 읽자

최종

  • 답이 존재하지 않으면 -1 을 반환해야 된다. 문제를 잘 읽자.
#include <string>
#include <vector>
#include <queue>
 
using namespace std;
 
int solution(vector<int> scoville, int K) {
	priority_queue<int, vector<int>, greater<int>> pq;
	int ret = 0;
	
	for (auto s : scoville) {
		pq.push(s);
	}
	
	while (pq.size() > 1) {
		int mixed;
		
		if (pq.top() >= K) {
			break;
		}
		
		mixed = pq.top();
		pq.pop();
 
		mixed += pq.top() * 2;
		pq.pop();
		
		pq.push(mixed);
		ret++;
	}
	
	if (pq.top() >= K) {
		return ret;
	}
	
	return -1;
}