반응형
스케줄러 같은 문제다.
신경써야 할 부분은 두가지가 있었다.
1. location에 해당하는 프로세스를 기억하고 있을 것
2. 프로세스가 빠진다면, 그 프로세스의 우선순위도 빠져야 한다는 것
두번째 것을 처음에 생각하지 못해 약간 삽질을 했다.
#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.emplace(i, priorities[i]);
pq.push(priorities[i]);
}
int cnt = 1;
while(1) {
pair<int, int> temp = q.front();
q.pop();
if(temp.second >= pq.top()) {
if(temp.first == location) {
return cnt;
}
pq.pop();
cnt++;
}
else {
q.push(temp);
}
}
}
풀이
1. priority_queue를 활용해 우선순위들을 정리
2. queue에는 pair를 넣어서 프로세스 ID와 우선순위를 모두 저장
3. 해당 프로세스의 우선순위가 pq의 top 이상이면 실행, 그것이 찾고있는 프로세스였다면 리턴
4. 아니라면 다시 큐에 삽입
반응형
'코딩테스트' 카테고리의 다른 글
[C++] 프로그래머스 - 이중우선순위큐 (0) | 2024.04.15 |
---|---|
[C++] 프로그래머스 - 기능개발 (0) | 2024.04.15 |
[C++][완전탐색] 프로그래머스 - 피로도 (0) | 2024.03.11 |
[C++][정렬] 프로그래머스 - 가장 큰 수 (0) | 2024.03.05 |
[C++][해시] 프로그래머스 - 완주하지 못한 선수 (0) | 2024.03.04 |