본문 바로가기
개발/Coding Test

[프로그래머스 Lv3 C++] 이중우선큐

by Eunduck 2022. 6. 7.
728x90

안녕하십니까. Eun🦆입니다.

풀이법 입니다.

 

 

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

 

programmers.co.kr

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<string> operations) {
    vector<int> answer;
    vector<int> queue;
    for(int idx=0;idx<operations.size();idx++)
    {
        string cmp = "";
        if(operations[idx]=="D 1")
        {
            if(queue.size()>1)
                sort(queue.begin(), queue.end(), greater<int>());
            //printf("D 1:%d\n",queue[0]);
            if(queue.empty()==0)
                queue.erase(queue.begin());
        }
        else if(operations[idx]=="D -1")
        {
            if(queue.size()>1)
                sort(queue.begin(), queue.end());
            //printf("D -1:%d\n",queue[0]);
            if(queue.empty()==0)
                queue.erase(queue.begin());
        }
        else
        {
            for(int i=2;i<operations[idx].size();i++)
                cmp += operations[idx][i];
            int n = atoi(cmp.c_str());
            //printf("+:%d\n",n);
            queue.push_back(n);
        }
    }
    if(queue.empty()==1)
    {
        answer.push_back(0);
        answer.push_back(0);
    }
    else
    {
        sort(queue.begin(), queue.end(), greater<int>());
        answer.push_back(queue[0]);
        sort(queue.begin(), queue.end());
        answer.push_back(queue[0]);
    }
    return answer;
}

 

네이버 블로그 리뉴얼입니다.

(https://blog.naver.com/unsuk1/222005318076)

728x90

댓글