Programmers - 이중 우선순위 큐

이준희·2022년 7월 12일

Algorithm

목록 보기
10/16

Programmers - 이중 우선순위 큐

처음에 내림차순 정렬 큐와 오름차순 정렬 큐 두개를 만들어서 count가 0일때,
즉, 큐에서 무언가를 다 뺀 경우에는 두개의 큐를 초기화 해주는 조건을 생각하지 못해
테스트 케이스 1번을 틀려버렸다...

앞으로 조건을 더 꼼꼼히 고려해봐야겠다..

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

using namespace std;

vector<int> solution(vector<string> operations) {
   vector<int> answer(2);
   priority_queue<int, vector<int>, greater<int>> pq_asc;
   priority_queue<int, vector<int>> pq_des;
   int cnt = 0;
   
   for(string op : operations) {
       if(!cnt) {
           while(!pq_asc.empty()) pq_asc.pop();
           while(!pq_des.empty()) pq_des.pop();
       }
       if(op[0] == 'I') {
           pq_des.push(stoi(op.substr(2)));
           pq_asc.push(stoi(op.substr(2)));
           cnt++;
       }
       else {
           if(cnt == 0) continue;
           if(op[2] == '1') {
               pq_des.pop();
               cnt--;
           }
           else {
               pq_asc.pop();
               cnt--;
           }
       }
   }
   
   if(cnt) {
       answer[0] = pq_des.top();
       answer[1] = pq_asc.top();
   }
   
   return answer;
}
profile
뉴비 개발자입니다!!

0개의 댓글