[프로그래머스/C++] 프린터

mani·2023년 4월 13일
0
post-custom-banner

프린터

max_element(v.begin(), v.end()), min_element()

#include <string>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

int solution(vector<int> priorities, int location) {
    queue<int> printer;                         //queue에 index 삽입.
    vector<int> sorted;                         //정렬된 결과 저장용
    for(int i=0; i<priorities.size(); i++) {
        printer.push(i);
    }
    while(!printer.empty()) {
        int now_index = printer.front();
        printer.pop();
        if(priorities[now_index] != *max_element(priorities.begin(),priorities.end())) {
            //아닌경우 push
            printer.push(now_index);
        } else {
            //맞는경우
            sorted.push_back(now_index);
            priorities[now_index] = 0;
        }
    }
    for(int i=0; i<sorted.size(); i++) {
        if(sorted[i] == location) return i+1;
    }
}
#include <string>
#include <vector>
#include <deque>

using namespace std;

int solution(vector<int> priorities, int location) {
    int answer = 0;
    deque<pair<int, int>> dq;
    for(int i=0;i<priorities.size();i++){
        dq.push_back(make_pair(i, priorities[i]));
    }
    
    while(!dq.empty()){
        pair<int, int> temp = dq[0];
        int size = dq.size();
        dq.pop_front();
        
        for(int i=0;i<size-1;i++){
        
            if(dq[i].second>temp.second){
                dq.push_back(temp);
                break;
            }
        }
        if(size != dq.size()){
            answer++;
            if(temp.first == location)
                return answer;
        } 
    }
    return answer;
}
profile
log
post-custom-banner

0개의 댓글