coding test - C++/Programmers

Programmers / 더 맵게 / C++ - 우선순위 큐

sillon 2026. 10. 3. 22:22
728x90
반응형

 

*문제 출처는 프로그래머스에 있습니다.

문제 제목: 더 맵게

문제 사이트: https://school.programmers.co.kr/learn/courses/30/lessons/42626

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 


나의 풀이

#include <string>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;

int solution(vector<int> scoville, int K) {
    int answer = 0;
    priority_queue<int, vector<int>,greater<int>> pq;
    
    for (int s:scoville){
        pq.push(s);
    }
    
    while (!pq.empty() && pq.top() < K){
        if (pq.size() < 2){
            return -1;
        }
        
        int t1 = pq.top();
        pq.pop();
        int t2 = pq.top();
        pq.pop();
        
        int result = t1 + (t2 * 2);
        
        pq.push(result);
        answer += 1;
    }
    
    
    return answer;
}

 

 


※ 알아야 할 것

- pq 는 for문으로 출력 못한다..

- 0인경우 작은 수의 경우도 잘 기억하자

 

728x90
반응형