c++/프로그래머스
프로그래머스 : 프린터 c++
호삐이
2020. 10. 27. 23:46
programmers.co.kr/learn/courses/30/lessons/42587
코딩테스트 연습 - 프린터
일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린
programmers.co.kr
풀이 및 후기
1. 처음에 while문 안에서 priorities 벡터 다 탐색하는 방식을 썻는데 시간초과!
2. a라는 우선순위 오른차순 벡터를 만들어서 쓸거임
-> answer를 index 처럼 쓰면서 뽑아도 되는 값인지 판단!
3. 뽑을 수 없는거면 대기목록 가장 맨뒤에 넣는거니까 queue 사용
4. 가장 높은 우선순위 값이 나왔으면 일단 뽑고, 내가 원하는 것인 경우 break;
5. return 할때는 +1 해서!
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int solution(vector<int> priorities, int location) {
int answer = 0;
queue<pair<int,int> >q; // index 랑 중요도
vector<int> a;
for(int i=0;i<priorities.size();i++)
{
q.push(make_pair(i,priorities[i]));
a.push_back(priorities[i]);
}
sort(a.begin(),a.end(),cmp);
while(!q.empty())
{
int index = q.front().first;
int priority = q.front().second;
q.pop();
if(a[answer] == priority)
{
if(location == index)
break;
answer++;
}
else
{
q.push(make_pair(index,priority));
}
}
return answer+1;
}