반응형
- 문제
location에 있는 인쇄 작업이 몇 번째로 인쇄되는지 출력하는 문제다.
- 풀이
#include <string>
#include <vector>
#include <queue>
#include <map>
using namespace std;
int solution(vector<int> priorities, int location) {
int answer = 0, prior = 9;
pair<int,int> temp;
bool next = true;
queue<pair<int,int>> qp;
map<int,int,greater<int>> m;
for(int i = 0; i < priorities.size(); i++){
qp.push(pair<int,int>(i,priorities[i]));
m[priorities[i]]++;
}
while(!qp.empty()){
if(next){
for(const auto& [key, value] : m){
if(value != 0){
prior = key;
m[key]--;
next = false;
break;
}
}
}
temp = qp.front();
if(temp.second == prior){
if(temp.first == location){
return ++answer;
}
next = true;
qp.pop();
answer++;
}
else{
qp.pop();
qp.push(temp);
}
}
return answer;
}
가장 중요도가 큰 문서가 나올때까지 pop하고 push를 반복하는 방식으로 (내가 생각할 때 조금 비효율적이지만, 다른 방법이 생각이 안나더라고요) 풀었다.
- 기억할 것!
X
반응형
'Algorithm > 프로그래머스' 카테고리의 다른 글
프로그래머스 주식가격 C++ (0) | 2022.10.22 |
---|---|
프로그래머스 타겟 넘버 C++ (0) | 2022.10.22 |
프로그래머스 [1차] 뉴스 클러스터링 (0) | 2022.10.22 |
프로그래머스 기능개발 C++ (0) | 2022.10.22 |
프로그래머스 위장 C++ (0) | 2022.10.22 |