반응형
programmers.co.kr/learn/courses/30/lessons/42587
코딩테스트 연습 - 프린터
일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린
programmers.co.kr
우선순위 큐와 큐를 사용해 푼 문제였습니다.
풀이방법
1. 우선순위 큐와 큐에 우선순위 정보를 push합니다.
2. 우선순위가 같다면 프린트 하고, 문서번호까지 같으면 정답입니다.
Code
#include <bits/stdc++.h>
using namespace std;
int solution(vector<int> priorities, int location) {
int answer = 0, cnt = 0;
queue<pair<int, int>> q;
priority_queue <int> pq;
for (int i = 0; i < priorities.size(); i++) {
q.push({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();
cnt++;
if (index == location) {
answer = cnt;
break;
}
continue;
}
q.push({index, value});
}
return answer;
}
'Algorithm > 자료구조' 카테고리의 다른 글
(C++) - 백준(BOJ) 17178번 : 줄서기 (0) | 2021.02.17 |
---|---|
(C++) - 프로그래머스(고득점 kit - 힙(heap)) : 이중우선순위큐 답 (0) | 2021.02.08 |
(C++) - 프로그래머스(고득점 kit - Hash) : 베스트앨범 답 (0) | 2021.02.02 |
(C++) - 프로그래머스(고득점 kit - Hash) : 위장 답 (0) | 2021.02.02 |
(C++) - 백준(BOJ) 11279번 : 최대 힙 답 (0) | 2020.09.11 |