음 개인적으로 너무 쉬웠다. 문제를 풀고 다른 사람의 풀이를 보면
max element
라는 요소를 사용했는데, 이러면 모든 부분에서 최댓값을 찾아 비교해야 하지 않나?? 라는 생각을 했다.
우선순위 큐
를 사용하여 최댓값을 갱신시켜줬다. 우선순위 큐
를 사용해도 되지만 일반 vector
를 사용하여 내림차순 정렬 을 시켜줘도 괜찮을 것이라고 본다.queue
를 사용해줬다.priority_queue
를 사용.우선순위 큐
의 top()
값과 큐
의 front()
값이 같다면 큐
의 값을 빼준다. 만약 자기가 찾는 값이라면 break
pair
값으로 순서와 값을 함께 넣어줬다.#include <string>
#include <vector>
#include <queue>
using namespace std;
int solution(vector<int> priorities, int location) {
int answer = 0;
queue<pair<int,int>>q;
priority_queue<int>pq;
for(int i=0;i<priorities.size();i++)
{
pq.push(priorities[i]);
q.push({priorities[i],i});
}
while(!q.empty())
{
if(q.front().first==pq.top())
{
answer++;
if(location == q.front().second) break;
else{
q.pop();
pq.pop();
}
}
else
{
q.push({q.front().first, q.front().second});
q.pop();
}
}
return answer;
}
q
값에 굳이priorities[i]
값을 집어넣지 않고 index 값만 넣어줘도 된다... 다른 사람의 풀이를 보고 깨달음..