문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/42587
코딩테스트 연습 - 프린터
일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린
programmers.co.kr
큐를 적절히 이용하면 쉽게 풀 수 있는 문제였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <queue> | |
using namespace std; | |
typedef struct | |
{ | |
int priority; | |
int idx; | |
} Document; | |
const int MAX = 10; | |
int solution(vector<int> priorities, int location) { | |
int morePriority[MAX] = { 0, }; | |
queue<Document> q; | |
for (int i = 0; i < priorities.size(); i++) | |
{ | |
for (int j = 1; j < priorities[i]; j++) | |
{ | |
morePriority[j]++; | |
} | |
q.push({ priorities[i], i }); | |
} | |
int answer = 0; | |
while (!q.empty()) | |
{ | |
Document cur = q.front(); | |
q.pop(); | |
if (morePriority[cur.priority]) | |
{ | |
q.push(cur); | |
continue; | |
} | |
answer++; | |
for (int j = 1; j < cur.priority; j++) | |
{ | |
morePriority[j]--; | |
} | |
if (cur.idx == location) | |
{ | |
return answer; | |
} | |
} | |
} | |
int main(void) | |
{ | |
vector<int> priorities = { 2, 1, 3, 2 }; | |
int location = 2; | |
cout << solution(priorities, location) << "\n"; | |
return 0; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers 코딩테스트 고득점 Kit] 주식가격 (0) | 2021.09.18 |
---|---|
[Programmers 코딩테스트 고득점 Kit] 다리를 지나는 트럭 (0) | 2021.09.18 |
[Programmers 위클리 챌린지 7주차] 입실 퇴실 (0) | 2021.09.14 |
[Programmers 코딩테스트 고득점 Kit] 기능개발 (0) | 2021.09.12 |
[Programmers 코딩테스트 고득점 Kit] 카펫 (0) | 2021.09.12 |