본문 바로가기
알고리즘 일기

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

by Beijing_KingGod 2019. 12. 10.

우선순위 큐 두개를 이용해 풀었다.

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

vector<int> solution(vector<string> operations) {
    vector<int> answer;
    priority_queue<int , vector<int>, greater<int> > minq;
    priority_queue<int , vector<int> , less<int> > maxq;
    int index = 0;
    for(auto i : operations){
        if(i[0] == 'I'){
            string num = i.substr(2);
            minq.push(stoi(num));
            index ++;
        }
        if(index>0&&i == "D 1"){ //최댓값삭제
            while(!minq.empty()){
                maxq.push(minq.top());
                minq.pop();
            }
            maxq.pop();
            while(!maxq.empty()){
                minq.push(maxq.top());
                maxq.pop();
            }
            index--;
        }
        if(index>0&&i=="D -1"){
            minq.pop();
            index--;
        }
    }
    if(minq.empty()){
        answer.push_back(0);
        answer.push_back(0);
        return answer;
    }
    answer.push_back(minq.top());
    while(minq.size()!=1){
        minq.pop();
    }
    answer.push_back(minq.top());
    reverse(answer.begin(),answer.end());
    return answer;
}

'알고리즘 일기' 카테고리의 다른 글

c++ map 사용법  (0) 2019.12.10
프로그래머스 : 저울  (0) 2019.12.10
프로그래머스 : 순위  (0) 2019.12.10
프로그래머스: 여행경로  (0) 2019.12.09
프로그래머스: 입국심사  (0) 2019.12.09

댓글