Stack/Queue_프로세스 (C++)

선풍기·2023년 12월 25일

프로그래머스

목록 보기
5/8

문제

자료구조/Stack_Queue 부분의 프로세스 문제이다.


풀이과정

처음에 문제를 이해하는데 많은 시간을 썼다.

하지만 이해하고 나니 그뒤는 쉽게 풀렸던 것 같다.

정답을 도출하기 위해

순서가 들어있는 vector 배열을 하나 만들기로 하고

vector 배열에 priorities의 크기만큼 다 채워진다면 while문을 빠져나오도록 설계했다.

그리고 sort함수를 이용해 priorities를 정렬한 vector을 만들어 주었다.

그렇게해서 반복문을 돌며 제일 큰것의 인덱스를 차례대로 order라는 벡터에 넣어주었다.

그리고 answer에서 찾고 싶은 인덱스를 find함수를 통해 도출했고, 이 배열은 0부터 시작하므로 +1을 하여 리턴해주었다.

정답코드

#include <string>
#include <vector>
#include <iostream>
#include<algorithm>
using namespace std;

int solution(vector<int> priorities, int location) {
    int answer = 0;
    vector<int> order;
    vector<int> great = priorities;
    sort(great.begin(), great.end(),greater<int>());
    int inx=0;
    int j = 0;
    while(order.size() != priorities.size()) {
        if (j >= priorities.size()) {
            j = 0;
        }
        if (priorities[j] == great[inx]) {
            order.push_back(j);
            inx++;
        }
        j++;
    }
    answer = find(order.begin(), order.end(), location)-order.begin()+1;
    return answer;
}

0개의 댓글