Make Unreal REAL.
article thumbnail
Level 3. 이중우선순위큐

 

 

출제자의 의도는 우선순위 큐 2개를 사용하라는 것 같은데 그냥 set으로 쉽게 풀었다.

  • 위와 같은 상황에서는 set을 사용하는 게 훨씬 간단하고 효율적이기 때문이다.

 

#include <iostream>
#include <vector>
#include <string>
#include <set>

using namespace std;

vector<int> solution(vector<string> operations)
{
    multiset<int> mset;
    
    for (string& op : operations)
    {
        int num = stoi(op.substr(2));

        switch (op[0])
        {
        case 'I':
            mset.emplace(num);
            break;
        case 'D':
            if (mset.empty())
                continue;
            else if (0 < num)
                mset.erase(*mset.rbegin());
            else
                mset.erase(mset.begin());
            break;
        default:
            break;
        }
    }

    return mset.empty() ? vector<int>{0, 0} : vector<int>{*mset.rbegin(), *mset.begin()};
}

'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글

Level 3. 단어 변환  (0) 2023.07.15
Level 3. 베스트앨범  (0) 2023.07.14
Level 3. 정수 삼각형  (0) 2023.07.12
Level 2. 거리두기 확인하기  (0) 2023.07.11
Level 0. 원소들의 곱과 합  (0) 2023.07.10
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그