본문 바로가기

problem solving

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

#include <string>
#include <vector>
#include <queue>
 
using namespace std;
 
vector<int> solution(vector<string> operations) {
    vector<int> answer;
    
    priority_queue<int> maxQ;
    priority_queue<intvector<int>, greater<int>> minQ;
    for(int idx = 0; idx < operations.size(); idx++) {
        if(operations[idx][0== 'I') {
            string N = "";
            for(int j = 2; j < operations[idx].size(); j++) {
                N += operations[idx][j];
            }
            int val = stoi(N);
            maxQ.push(val);
            minQ.push(val);
        }
        else if(operations[idx][0== 'D') {
            if(operations[idx][2== '1') {
                if(!maxQ.empty()) {
                    if(maxQ.top() == minQ.top()) {
                        minQ.pop();
                    }
                    maxQ.pop();
                    
                    if(maxQ.top() < minQ.top()) {
                        while(!maxQ.empty()) {
                            maxQ.pop();
                        }
                        while(!minQ.empty()) {
                            minQ.pop();
                        }
                    } 
                }
            }
            else if(operations[idx][2== '-') {
                if(!minQ.empty()) {
                    if(maxQ.top() == minQ.top()) {
                        maxQ.pop();
                    }
                    minQ.pop();
                    if(maxQ.top() < minQ.top()) {
                        while(!maxQ.empty()) {
                            maxQ.pop();
                        }
                        while(!minQ.empty()) {
                            minQ.pop();
                        }
                    } 
                }
            }
        }
    }
    
    int max = maxQ.empty() == 1 ? 0 : maxQ.top();
    int min = minQ.empty() == 1 ? 0 : minQ.top();
    answer.push_back(max);
    answer.push_back(min);
    return answer;
}
cs

'problem solving' 카테고리의 다른 글

[프로그래머스]디스크 컨트롤러  (0) 2020.03.14
[프로그래머스]섬 연결하기  (0) 2020.03.13
[미완]디스크 분할  (0) 2020.03.13
[프로그래머스]라면공장  (0) 2020.03.13
[프로그래머스]라면공장  (0) 2020.03.13