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

lsh235·2024년 12월 4일
0

CodingTest

목록 보기
21/31

문제 : https://school.programmers.co.kr/learn/courses/30/lessons/42628
..? max_elemet로 풀면 안되나? [다시확인필요]


#include <algorithm>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<string> operations) {
    vector<int> queue;
    for (const auto oper : operations) {
        if ("D -1" == oper) {
            if (queue.empty() == false)
                queue.erase(min_element(queue.begin(), queue.end()));
        } else if ("D 1" == oper) {
            if (queue.empty() == false)
                queue.erase(max_element(queue.begin(), queue.end()));
        } else {
            if (oper[0] == 'I') {
                int num = stoi(oper.substr(2));
                queue.emplace_back(num);
            }
        }
    }

    vector<int> answer;
    if (queue.empty() == false) {
        answer.emplace_back(*max_element(queue.begin(), queue.end()));
        answer.emplace_back(*min_element(queue.begin(), queue.end()));
    } else {
        answer.emplace_back(0);
        answer.emplace_back(0);
    }
    return answer;
}

0개의 댓글