Study Record

[프로그래머스] Level2 - 프린터 <Queue> 본문

알고리즘

[프로그래머스] Level2 - 프린터 <Queue>

초코초코초코 2021. 12. 4. 17:00
728x90
#include <string>
#include <vector>
#include <queue>

using namespace std;

int solution(vector<int> priorities, int location) {
    int answer = 0;
    int biggerNum[10] = { 0 };
    queue<int> waitting;

    for (int i = 0; i < priorities.size(); i++) {
        biggerNum[priorities[i]]++;
        if (location == i) waitting.push((-1) * priorities[i]);
        else waitting.push(priorities[i]);
    }

    for (int i = 9; i > 0; i--) {
        if (biggerNum[i] == 0) continue;
        int count = biggerNum[i];
        while (count > 0) {
            int num = waitting.front();
            waitting.pop();
            if (num == i) {
                answer++;
                count--;
            }
            else if (num * (-1) == i) {
                answer++;
                break;
            }
            else {
                waitting.push(num);
            }
        }
        if (count > 0) break;
    }
    return answer;
}
728x90