본문 바로가기

코딩테스트/프로그래머스

[프로그래머스] 프린터

프로그래머스 프린터 C++ 풀이

 

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

 

 

// 우선순위 큐 pq에 priorities 넣고 그냥 큐q에 priorities와 index 넣는다.
// 그리고 while문에서 q에서 priorities와 index 빼서 pq의 top과 비교해서 같으면 answer++ 하고 이 인덱스가 location이랑 같으면 내가 원하던 위치니까 break하고 아니면 다시 큐에 p와 priorities 넣는다. (그래야 순환되니까)
#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;
    
    for (int i = 0; i < priorities.size(); i++){
        pq.push(priorities[i]);
        q.push({priorities[i], i});
    }
    
    while(!q.empty()){
        
        int priority = q.front().first;
        int index = q.front().second;
        q.pop();
        
        if (priority == pq.top()){
            pq.pop();
            answer++;
            
            if (index == location)
                break;
            
        }
        
        q.push({priority, index});
        
    }
    
    return answer;
}