[프로그래머스] 프로세스 - c++

삼식이·2025년 5월 11일

알고리즘

목록 보기
50/84

프로세스

프로세스를 우선순위가 높은 순으로 나열하기 위해 우선순위 큐에 우선순위만을 넣었고,

큐에는 우선순위와, 해당 인덱스 위치를 pair로 담았다.

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

using namespace std;

int solution(vector<int> priorities, int location) {
    queue<pair<int, int>> q;
    priority_queue<int> pq;

    for (int i = 0; i < priorities.size(); i++) {
        q.push({priorities[i], i});
        pq.push(priorities[i]);
    }

    int answer = 0;

    while (!q.empty()) {
        int cur_priority = q.front().first;
        int cur_index = q.front().second;
        q.pop();

        if (cur_priority == pq.top()) {
            answer++;
            pq.pop();
            if (cur_index == location) {
                return answer;
            }
        } else {
            q.push({cur_priority, cur_index});
        }
    }

    return answer;
}

0개의 댓글