[프로그래머스 / C++] 프린터

Seulguo·2022년 7월 6일
0

Algorithm

목록 보기
12/185
post-thumbnail
post-custom-banner

🐣 문제

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


🐥 코드

#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++){
        q.push(make_pair(i,priorities[i]));
        pq.push(priorities[i]);
    }
    
    while(!q.empty()){
        int index = q.front().first;
        int priority = q.front().second;
        q.pop();
        
        if (priority == pq.top()){
            pq.pop();
            answer += 1;
        
            if(index == location) break;
        }
        else{
            q.push(make_pair(index, priority));
        }
    }
    
    return answer;
}
post-custom-banner

0개의 댓글