반응형

 

 

 

 

풀이코드

#include <string>
#include <vector>
#include <queue>

using namespace std;

int GetAnswer(const vector<int>&, int);

int solution(vector<int> priorities, int location) {
    int answer = GetAnswer(priorities, location);
    return answer;
}

int GetAnswer(const vector<int>& priorities, int location)
{
    int result = 0;
    queue<pair<int, int>> iQueue;
    priority_queue<int> pQueue;
    for(int i = 0; i < priorities.size(); i++)
    {
        iQueue.push(make_pair(i, priorities[i]));
        pQueue.push(priorities[i]);
    }

    while(!iQueue.empty())
    {
        if(iQueue.front().second == pQueue.top())
        {
            pQueue.pop();
            result++;

            if(iQueue.front().first == location)
            {
                break;
            }
        }
        else
        {
            iQueue.push(iQueue.front());
        }
        iQueue.pop();
    }

    return result;
}
  • vector의 값을 queue<pair<int, int>와 priority_queue<int>에 넣어준다.
  • 그 뒤, 큐의 priority값과 우선순위 큐의 값을 비교해서 같으면 result를 증가시키고 다르면 큐의 첫번째 값을 맨 뒤로 옮겨준다.
  • priority 값이 같을 때, location값을 비교하는데 location 값이 같으면 내가 요청한 문서를 찾은것이니 반복문을 나와 result 값을 반환한다.

 

 

 

기타

- 인수로 들어오는 vector 값으로 해결해보려고 했는데, 기본 케이스는 통과가 되나 제출 시의 테스트 케이스에서 불합격이 나는바람에 포기했다. (어떤 케이스에서 안되는지 알고싶은데 그걸 모르니 답답함...)

 

 

 

 

 

반응형

'Stack > Coding test' 카테고리의 다른 글

[C# / Lv2] 주식 가격  (0) 2022.02.26
[C++ / Lv 2] 다리를 지나는 트럭  (0) 2022.02.26
[C++ / Lv2] 기능개발  (0) 2022.02.15
[C++ / Lv3] 베스트 앨범  (0) 2022.02.10
[C++ / Lv2] 위장  (0) 2022.02.07

+ Recent posts