프로그래머스 프린터 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;
}
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 주식가격 (0) | 2023.07.07 |
---|---|
[프로그래머스] 쇠막대기 (0) | 2023.07.07 |
[프로그래머스] 기능개발 (0) | 2023.07.07 |
[프로그래머스] 다리를 지나는 트럭 (0) | 2023.07.07 |
[프로그래머스] 탑 (0) | 2023.07.07 |