[프로그래머스 42587] 프로세스
AlgorithmQueue
- 출처: 프로그래머스 42587
- 분류: 스택/큐 · Lv.2
문제
우선순위 배열 priorities와 위치 location이 주어진다. 큐에서 하나를 꺼낸 뒤, 더 높은 우선순위가 남아있으면 다시 큐에 넣는다. 없으면 실행한다. location 프로세스가 몇 번째로 실행되는지 반환하라.
핵심
큐 시뮬레이션. 큐에서 꺼낸 뒤 남은 원소 중 최대값과 비교해서 실행 여부를 결정한다. 원래 인덱스를 함께 저장해야 location과 대조할 수 있다.
풀이
#include <queue>
#include <vector>
using namespace std;
int solution(vector<int> priorities, int location) {
queue<pair<int, int>> q; // {우선순위, 원래 인덱스}
for (int i = 0; i < priorities.size(); i++)
q.push({priorities[i], i});
int order = 0;
while (!q.empty()) {
auto [pri, idx] = q.front();
q.pop();
// 더 높은 우선순위가 남아있는지 확인
bool hasHigher = false;
int sz = q.size();
for (int i = 0; i < sz; i++) {
if (q.front().first > pri) hasHigher = true;
q.push(q.front());
q.pop();
}
if (hasHigher) {
q.push({pri, idx});
} else {
order++;
if (idx == location) return order;
}
}
return order;
}
메모
pair<int, int>로 우선순위와 원래 인덱스를 묶는 이유: 큐에서 위치가 계속 바뀌므로 원래 인덱스를 기억해야location과 비교할 수 있다.- 최대값 확인을 큐 순회로 하는 이유:
queue는 임의 접근이 안 된다. 한 바퀴 돌면서 확인하고 다시 넣는 방식.priority_queue나max_element를 별도로 쓰는 방법도 있다. auto [pri, idx] = q.front(): C++17 구조화 바인딩.pair.first/second대신 의미 있는 이름으로 접근.
max_element로 간결하게
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
int solution(vector<int> priorities, int location) {
deque<pair<int, int>> dq;
for (int i = 0; i < priorities.size(); i++)
dq.push_back({priorities[i], i});
int order = 0;
while (!dq.empty()) {
auto [pri, idx] = dq.front();
dq.pop_front();
if (any_of(dq.begin(), dq.end(), [&](auto& p) { return p.first > pri; })) {
dq.push_back({pri, idx});
} else {
order++;
if (idx == location) return order;
}
}
return order;
}
deque는 iterator를 지원해서 any_of로 한 줄에 확인 가능. queue는 iterator가 없다.
복잡도
| 시간 | 공간 |
|---|---|
| O(N²) | O(N) |
매 실행마다 큐 전체를 순회하므로 O(N²). N이 최대 100이라 문제없다.