priority_queue 와 queue 모두 써서 푸는 문제였다.
q로 대기열을 계속 바꾸고 pq로 출력 할 지 안할지 확인한다.
#include <string>
#include <vector>
#include <queue>
using namespace std;
int solution(vector<int> priorities, int location) {
int answer = 0, count = 0;
queue<pair<int, int>> q;
priority_queue <int> pq;
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 value = q.front().second;
q.pop();
if (pq.top() == value) {
pq.pop();
count++;
if (index == location) {
answer = count;
break;
}
}
else
q.push(make_pair(index, value));
}
return answer;
}'Coding_Test 연습 > Programmers' 카테고리의 다른 글
| [프로그래머스] (C++) LV2 소수 찾기 (0) | 2022.05.23 |
|---|---|
| [프로그래머스] (C++) LV2 가장 큰 수 (0) | 2022.05.22 |
| [프로그래머스] (C++) LV2 전화번호 목록 (0) | 2022.05.21 |
| [프로그래머스] (C++) LV2 빛의 경로 사이클 (0) | 2022.05.19 |
| [프로그래머스] (C++) LV2 행렬 테두리 회전하기 (0) | 2022.05.19 |