알고리즘/프로그래머스

프로그래머스 - 이중우선순위큐

시나모온 2020. 9. 1. 15:39

문제 링크입니다 : https://programmers.co.kr/learn/courses/30/lessons/42628

 

코딩테스트 연습 - 이중우선순위큐

 

programmers.co.kr

 

 

우선순위큐.. 

 

설명 생략!

 

 

 

 

#include <string>
#include <vector>
#include <queue>
#include <iostream>

using namespace std;

vector<int> solution(vector<string> operations) {
    vector<int> answer;
    
    int cnt = 0;
    priority_queue<int> pq;
    
    for(int i = 0; i < operations.size(); i++) {
        string str = operations[i];
        if(str[0] == 'I') {
            int num = stoi(str.substr(2));
            pq.push(num);
        } else if(str[2] == '-') {
            if(pq.size() > cnt) {
                cnt++;
            }
        } else {
            if(pq.size() > cnt) {
                pq.pop();
            }
        }
    }
    
    if(pq.size() <= cnt) {
        answer = {0, 0};
    } else {
        
        
        int maxVal = pq.top();
        int minVal = pq.top();
        
        while(pq.size() > cnt) {
            int top = pq.top();
            cout << top << endl;
            
            if(maxVal < top) {
                maxVal = top;
            }
            if(minVal > top) {
                minVal = top;
            }
            pq.pop();
        }
        answer = {maxVal, minVal};
    }
    
    return answer;
}

 

 

 

개발 환경 : vscode

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~