https://school.programmers.co.kr/learn/courses/30/lessons/42587
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
우선순위 큐와 큐를 사용해서 해결할 수 있다.
우선순위 큐에는 우선순위 대로 실행 할 프로세스를 저장한다.
큐에는 프로세스의 우선순위와 인덱스를 저장한다.
우선순위 큐에서 요소 하나를 뽑고 큐를 탐색한다.
만약 큐에서 뽑은 요소가 우선순위 큐에서 뽑은 요소와 다른 경우 큐에 다시 넣는다.
같은 경우 인덱스를 비교해 검색할 프로세스 번호와 같은 경우 답을 반환한다.
다르면 다음 반복으로 넘어간다.
#include <string>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
int solution(vector<int> priorities, int location) {
int answer = 1;
priority_queue<int> pq;
queue<pair<int,int>> q;
for(int i = 0;i<priorities.size();i++){
pq.push(priorities[i]);
q.push({priorities[i],i});
}
while(!pq.empty()){
int now = pq.top();
pq.pop();
while(!q.empty()){
int process = q.front().first;
int idx = q.front().second;
q.pop();
if(process != now){
q.push({process,idx});
}
else{
if(idx == location)
return answer;
break;
}
}
answer++;
}
return answer;
}'Programmers Review' 카테고리의 다른 글
| [Lv 2] 가장 큰 정사각형 찾기 (0) | 2024.06.28 |
|---|---|
| [Lv 2] 기능개발 (0) | 2024.06.28 |
| [Lv 2] 더 맵게 (0) | 2024.06.28 |
| [Lv 2] H-Index (0) | 2024.06.28 |
| [Lv 2] 카펫 (1) | 2024.06.28 |