[Programmers] 프린터

Date:

[Programmers] 프린터

Problem URL : 프린터

#include <vector>
#include <queue>

using namespace std;

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

    int size = priorities.size();
    for (int i = 0; i < 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 (value == pq.top()) {
            // 제일 큰 값이면 프린트한다.
            pq.pop();
            answer++;
            if (index == location) {
                break;
            }
        } else {
            q.push({ index, value });
        }
    }

    return answer;
}

댓글