본문 바로가기

개발/알고리즘

프로그래머스 더 맵게 C++

문제

 

풀이

#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 answer = 0;
    
    for(int i = 0; i < scoville.size(); i++)
    {
        pq.push(scoville[i]);
    }
    
    int first(0), second(0);
    while(pq.top() < K)
    {
        if(pq.size() <= 1)
            return -1;
        
        first = pq.top();
        pq.pop();
        
        second = pq.top();
        pq.pop();
        
        first = first + second * 2;
        
        pq.push(first);
        
        answer++;
    }
        
    return answer;
}

 

결과

 

걸린 시간

40분