https://school.programmers.co.kr/learn/courses/30/lessons/42587
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
중요도, 인덱스 정보를 한 쌍으로 가지는 큐
큐의 front 값이 최우선 순위인지 확인하기 위해 사용하는 우선순위큐
두가지를 사용해 풀이
우선 for문으로 큐(q)와 우선순위큐(pq)에 정보 push
우선순위큐의 top 에는 가장 값이 큰 데이터가 옴
따라서 현 시점에서 가장 높은 중요도를 찾을 수 있음
q의 front 값, 즉
q.front().first (중요도)
q.front().second (인덱스)
정보를 pq.front() 와 비교하여 q.front가 현 시점에서 가장 중요한 문서가 아니라면 해당 데이터를 다시 큐 뒤로 push
가장 중요한 문서라면 우선순위큐 pop 실행하여 다음 중요도 탐색 후 카운터 (cnt) 증가
여기서 만약 q.front().second (인덱스) 값이 내가 찾고자 했던 값, location의 값과 동일하다면 break로 while 반복문 탈출
int solution(vector<int> priorities, int location) {
int answer = 0;
int cnt = 0;
int size = priorities.size();
queue<pair<int, int>> q;
priority_queue<int> pq;
for (int i = 0; i < priorities.size(); i++) {
q.push(make_pair(priorities[i], i));
pq.push(priorities[i]);
}
while(!q.empty()) {
int val = q.front().first;
int idx = q.front().second;
q.pop();
if (pq.top()==val) {
pq.pop();
cnt++;
if (idx == location)
break;
}
else {
q.push(make_pair(val, idx));
}
}
answer = cnt;
return answer;
}
'코딩테스트 > 문제풀이' 카테고리의 다른 글
[프로그래머스] 주식가격 C++ (0) | 2022.08.14 |
---|---|
[프로그래머스] 다리를 지나는 트럭 C++ (0) | 2022.08.14 |
[프로그래머스] 기능개발 C++ (0) | 2022.08.10 |
[프로그래머스] 공백 문자열 C++ (0) | 2022.06.01 |
[프로그래머스] 최소공배수 C++ (0) | 2022.06.01 |