Algorithm
[Algorithm] 프로그래머스 C++ : 프린터
dokylee
2020. 8. 9. 02:08

https://programmers.co.kr/learn/courses/30/lessons/42587#
코딩테스트 연습 - 프린터
일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린��
programmers.co.kr
그냥 프린터의 인덱스를 스택에 넣고 조건에 맞게 push&pop하면 된다. (우선순위값은 priorities[index]이런 식으로 접근하여 비교)
초반에 몇개 놓쳤다가 다시 생각한게
우선순위가 높은 출력물이 출력이 된 이후(큐에서 pop된 이후)에 가장 큰 우선순위값을 다시 구해줘야 한다는 점!
그래서 while가지고 최댓값을 매번 pop 할 때마다 구해줬다.
조건을 if/else로 크게크게 나눈 후에 디테일 코딩을 하는게 좋았을 듯.
Code
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
int solution(vector<int> priorities, int location) {
int answer = 1;
queue<int> q;
for(int i=0; i<priorities.size(); i++) {
q.push(i);
}
int max = *max_element(priorities.begin(), priorities.end());
while(1) {
if(max != priorities[q.front()]) {
int tmp = q.front();
q.pop();
q.push(tmp);
}
else {
if(q.front()==location) {
break;
}
else {
q.pop();
answer++;
max = 0;
int iter = q.size();
while(iter--) {
int tmp = q.front();
if(max < priorities[tmp]) {
max = priorities[tmp];
}
q.pop();
q.push(tmp);
}
}
}
}
return answer;
}