프로그래머스 알고리즘/2단계
(프로그래머스 c++)프린터
naeunchan
2020. 5. 4. 11:42
728x90
반응형

큐 형태를 생각하면서 풀면 되는 문제..!
큐를 선언하면 되지만 for문 돌리면서 큐에 넣기 귀찮아서 그냥 벡터로 하였다...
tmp 벡터에 priorities를 넣어주고, 내림차순으로 tmp를 정렬한다.
그리고 tmp.front()와 priorities.front()가 같지 않으면
location--를 해준다.(location == 0 이면, priorities.size() - 1을 해준다..!)
그 후 priorities.front()를 맨 마지막에 넣어준다.
만약 priorities.front()와 tmp.front()가 같고, location == 0이면 내가 요청한 문서가 프린터 되는 시점이다.
바로 return answer을 해주면 된다.
하지만 location != 0이면 내가 요청한 순서가 아니기 때문에 tmp와 priorities의 맨 앞의 요소를 지워준다.
이를 계속 반복하여 location == 0 일 때까지 돌면 끝..!
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool desc(int a, int b)
{
return a > b;
}
int solution(vector<int> priorities, int location) {
int answer = 1;
vector<int> tmp = priorities;
sort(tmp.begin(), tmp.end(), desc);
while(1)
{
if(tmp.front() != priorities.front())
{
if(location == 0)
location = priorities.size() - 1;
else
location--;
priorities.push_back(priorities.front());
priorities.erase(priorities.begin());
}
else
{
if(location == 0)
return answer;
else
{
answer++;
tmp.erase(tmp.begin());
priorities.erase(priorities.begin());
location--;
}
}
}
return answer;
}
728x90
반응형