hwooo
프로그래머스 (C/C++) 42587 : 프로세스 본문
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;
}
'Study > Algorithm' 카테고리의 다른 글
| 프로그래머스 (C/C++) 42892 : 길 찾기 게임 (0) | 2023.08.30 |
|---|---|
| BOJ (C/C++) 5639번: 이진 검색 트리 (0) | 2023.08.30 |
| 프로그래머스 (C/C++) 12914 : 멀리 뛰기 (0) | 2023.08.20 |
| 프로그래머스 (C/C++) 76502 : 괄호 회전하기 (0) | 2023.08.20 |
| 프로그래머스 (C/C++) 120838 : 모스부호(1) (0) | 2023.08.17 |