프로그래머스알고리즘

힙 - 더맵게 우선순위 큐 사용

풀스택 개발자 2021. 4. 27. 16:42
#include <string>
#include <vector>
#include <queue>
using namespace std;

int solution(vector<int> scoville, int K) {

	int answer = 0;
	int sum = 0;
    
	priority_queue<int, vector<int>, greater<int> > pq(scoville.begin(), scoville.end());
	
	while(1)
	{
		sum = 0;
		if(pq.top()>K)
		{
			return answer;
			break;
		}
		if(pq.size()==1&&pq.top()<K)
		{
			return -1;
		}
		int first = pq.top();
		pq.pop();
		int second = pq.top();
		pq.pop();
		sum = first + (second*2);
		pq.push(sum);
		answer++;
	}
	
}