[프로그래머스 문제풀이] 14. 이중우선순위큐

WIGWAG·2023년 1월 3일
0

프로그래머스

목록 보기
14/32

중복을 허용하면서 정렬해야 하기 때문에 multiset을 사용하여 문제를 풀었다.


stringstream으로 명령어 안에 있는 알파벳과 숫자를 쉽게 분리할 수 있다.

stringstream ss(str);
ss >> order_code;
ss >> value;

#include <string>
#include <vector>
#include <set>
#include <sstream>

#include <iostream>

using namespace std;

vector<int> solution(vector<string> operations) {
    multiset<int> values;
    char order_code;
    int value;

    for (string& str : operations)
    {
        stringstream ss(str);
        ss >> order_code;
        ss >> value;

        if (order_code == 'I')
            values.insert(value);
        else
        {
            if (values.empty()) continue;

            if (value == 1)
                values.erase(values.end() - 1);
            else
                values.erase(values.begin());
        }
    }

    if (values.empty())
        return { 0,0 };
    else
        return{ *(values.end() - 1), *values.begin() };
}

int main()
{
    for (auto d : solution({ "I 16", "I -5643", "D -1", "D 1", "D 1", "I 123", "D -1" }))
    {
        cout << d << ' ';
    }
}

실행결과

0 0


이중우선순위큐 문제 링크

profile
윅왁의 프로그래밍 개발노트

0개의 댓글