프로그래머스 문제풀이/LEVEL 2

[C++] 프로그래머스 문제풀이 LEVEL 2 더 맵게

코딩준우 2023. 7. 5. 12:23

 

#include <bits/stdc++.h>
using namespace std;

int solution(vector<int> scoville, int K)
{
    int answer = 0;

    priority_queue<int, vector<int>, greater<int>> pq(scoville.begin(), scoville.end());

    while(pq.top() < K)
    {
        if (pq.size() == 1) 
        {
            return -1;
        }
        
        int n1 = pq.top();
        pq.pop();
        int n2 = pq.top();
        pq.pop();

        pq.push(n1 + n2 * 2);
        ++answer;
    }
    return answer;
}