프로그래머스(Programmers)

스택/큐 > 프린터

cepiloth 2018. 9. 30. 20:36
반응형

https://programmers.co.kr/learn/courses/30/lessons/42587


1. 문제


2. 알고리즘

키워드 - 우선순위큐


3. 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
 
using namespace std;
 
int solution(vector<int> priorities, int location) {
    int answer = 0;
    int count = 0;
    
    queue<pair<intint>> q;
    priority_queue <int> pq;
 
    for (int i = 0; i < priorities.size(); i++) {
        q.push(make_pair(i, priorities[i]));
        pq.push(priorities[i]);
    }
    
    while (!q.empty()) {
        int c_index = q.front().first;
        int c_value = q.front().second;
        q.pop();
 
        if (pq.top() == c_value) {
            pq.pop();
            count++;
            if (c_index == location) {
                answer = count;
                break;
            }
        }
        else {
            q.push(make_pair(c_index, c_value));
        }
    }
    return answer;
}
cs

반응형