알고리즘/프로그래머스

Programers 프린터

내이름은 킹햄찌 2022. 3. 12. 22:22

https://programmers.co.kr/learn/courses/30/lessons/42587

 

코딩테스트 연습 - 프린터

일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린

programmers.co.kr

프로그래머스 프린터 문제입니다.

#include <string>
#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 pos =0;
    for(auto iter : priorities){
        pq.push(iter);
        q.push({iter,pos++});
    }
    
    while(!q.empty()){
        int priority = q.front().first;
        int pos = q.front().second;
        q.pop();
        if(pq.top() != priority){
            q.push({priority,pos});
            continue;
        }
        pq.pop();
        answer++;
        if(location == pos)
            break;
    }
    
    return answer;
}

 

아이디어

현재 문서에서 중요도가 높은 문서가 뒤에 있을때 먼저 출력할 수 없고, 작업의 가장 마지막으로 보낸다는 것은

대놓고 우선큐를 설명

priority_queue를 이용해여 우선순위 정렬을 먼저 한 후 현재의 작업이 우선큐의 front와 같지 않다면 뒤로 보내면 됨