Notice
Recent Posts
Recent Comments
Link
«   2025/12   »
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
Archives
Today
Total
관리 메뉴

hwooo

프로그래머스 (C/C++) 42587 : 프로세스 본문

Study/Algorithm

프로그래머스 (C/C++) 42587 : 프로세스

hwooo 2023. 8. 27. 17:16

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

 

프로그래머스

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

programmers.co.kr


풀이

큐에 현재 입력된 벡터의 값을 인덱스와 함께 저장해줬다.

큐는 인덱스로 접근이 안 되므로 priorities에서 가장 큰 값을 찾았고, 이 때 해당되는 인덱스 값도 같이 return했다.

큐에서 해당 값이 나올 때까지 push, pop을 반복하고 이 때 꺼내야 될 값이 location과 같으면 answer을 반환했다.


코드

#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

pair<int,int> Find_first_pop(vector<int> P){
    int maximum = 0, max_i = -1;
    for(int i = 0; i < P.size(); i++){
        if(maximum < P[i])
            maximum = P[i], max_i = i;
    }
    return {maximum, max_i};
}

int solution(vector<int> priorities, int location) {
    int answer = 0;
    queue <pair<int,int>> q;
    pair<int,int> now, Fir;

    for(int i=0;i<priorities.size();i++)
        q.push({i, priorities[i]});
    
    while(!q.empty()){
        Fir = Find_first_pop(priorities);
        
        while(q.front().second != Fir.first){
            q.push(q.front());
            q.pop();
        }
        
        answer++;
        if(q.front().first == location)
            return answer;
        
        q.pop();
        priorities.erase(priorities.begin() + Fir.second);
    }
    return answer;
}