코딩 테스트
프로그래머스 - 더 맵게
__sapar
2024. 8. 9. 15:58
pop 할 때는 조심히 하자. 굳이 비어있을때만을 조건의 끝으로 보지 않아도 된다. 좀 더 쉽게 접근해도 괜찮다. 그게 오히려 잘만든거임
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
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.size() > 1 && pq.top() < K)
{
int one = pq.top();
pq.pop();
if(one >= K )
{
break;
}
int two = pq.top();
pq.pop();
int newS = one + (two +two);
pq.push(newS);
answer++;
}
if(pq.top() < K)
{
answer = -1;
}
return answer;
}