priority_queue 와 queue 모두 써서 푸는 문제였다.

q로 대기열을 계속 바꾸고 pq로 출력 할 지 안할지 확인한다.

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

using namespace std;

int solution(vector<int> priorities, int location) {
    int answer = 0, count = 0;
    queue<pair<int, int>> q;
    priority_queue <int> pq;

    for (int i = 0; i < priorities.size(); i++) {
        q.push(make_pair(i, priorities[i]));
        pq.push(priorities[i]);
    }

    while (!q.empty()) {
        int index = q.front().first;
        int value = q.front().second;
        q.pop();

        if (pq.top() == value) {

            pq.pop();

            count++;

            if (index == location) {
                answer = count;
                break;
            }
        }
        else
            q.push(make_pair(index, value));
    }
    return answer;
}

+ Recent posts