본문 바로가기

Algorithm/프로그래머스

프로그래머스 프린터 C++

반응형

프로그래머스 : 프린터

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 


- 문제

  location에 있는 인쇄 작업이 몇 번째로 인쇄되는지 출력하는 문제다.

 


- 풀이

#include <string>
#include <vector>
#include <queue>
#include <map>

using namespace std;

int solution(vector<int> priorities, int location) {
    int answer = 0, prior = 9;
    pair<int,int> temp;
    bool next = true;
    queue<pair<int,int>> qp;
    map<int,int,greater<int>> m;
    for(int i = 0; i < priorities.size(); i++){
        qp.push(pair<int,int>(i,priorities[i]));
        m[priorities[i]]++;
    }
    while(!qp.empty()){
        if(next){
            for(const auto& [key, value] : m){
                if(value != 0){
                    prior = key;
                    m[key]--;
                    next = false;
                    break;
                }
            }
        }
        temp = qp.front();
        if(temp.second == prior){
            if(temp.first == location){
                return ++answer;
            }
            next = true;
            qp.pop();
            answer++;
        }
        else{
            qp.pop();
            qp.push(temp);
        }
    }
    return answer;
}

 

 

가장 중요도가 큰 문서가 나올때까지 pop하고 push를 반복하는 방식으로 (내가 생각할 때 조금 비효율적이지만, 다른 방법이 생각이 안나더라고요) 풀었다.

 

  


- 기억할 것!

 X

반응형