#include <string> #include <vector> #include <queue> #include <algorithm> using namespace std; int solution(vector<int> priorities, int location) { queue<pair<int, int>> q; // 우선순위, 인덱스 for(int i = 0; i < priorities.size(); i++) { q.push({priorities[i], i}); } // 최댓값을 구하기 위해 정렬 sort(priorities.begin(), priorities.end()); int cnt = 1; while(!q.empty()) { int cmp = q.front().first; int cmpIdx = q.front().second; // 맨 앞 대기목록이 쵀댓값이 아닌 경우, 맨 뒤로 if(cmp < priorities[priorities.size()-1]) { pair<int, int> temp = q.front(); q.pop(); q.push(temp); } // 맨 앞 대기목록이 최댓값인 경우 else { // 찾는 location인 경우 if(cmpIdx == location) { break; } // 찾는 location이 아닌 경우 else { q.pop(); priorities.pop_back(); cnt++; } } } return cnt; } | cs |
'Programmers > Level 2' 카테고리의 다른 글
[프로그래머스 2] 소수 찾기 (C/C++) (0) | 2019.10.22 |
---|---|
[프로그래머스 2] 큰 수 만들기 (C/C++) (★★) (0) | 2019.10.22 |
[프로그래머스 2] 탑 (C/C++) (1) | 2019.10.21 |
[프로그래머스 2] 124 나라의 숫자 (C/C++) (★) (0) | 2019.10.21 |
[프로그래머스 2] 주식가격 (C/C++) (0) | 2019.10.20 |