[프로그래머스 / C++] 이중우선순위큐

Seulguo·2022년 7월 7일
0

Algorithm

목록 보기
22/185
post-thumbnail
post-custom-banner

🐣 문제

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42628


🐥 코드

#include <string>
#include <vector>
#include <queue> 
#include <algorithm>

using namespace std;

vector<int> solution(vector<string> operations) {
    vector<int> answer;
    priority_queue<int, vector<int>, less<int>> pq_max;
    priority_queue<int, vector<int>, greater<int>> pq_min;
    int size = 0; 
    
    for(int i = 0; i < operations.size(); i++){
        if(operations[i][0] == 'I'){
            pq_max.push(stoi(operations[i].substr(2, operations[i].length())));
            pq_min.push(stoi(operations[i].substr(2, operations[i].length())));
            size ++;
        }
        else if((operations[i][0] == 'D') && size > 0){
            if(operations[i][2] == '1'){
                pq_max.pop();
                size --;
            }
            else{
                pq_min.pop();
                size --;
            }
            if(size == 0){
                while(!pq_min.empty()) pq_min.pop();
                while(!pq_max.empty()) pq_max.pop();
            }
        }
    }
    
    if(size <= 0){
       answer = {0, 0};
    }
    
    if(!pq_min.empty() && !pq_max.empty()) answer = {pq_max.top(), pq_min.top()};
    return answer;
}
post-custom-banner

0개의 댓글