반응형
https://school.programmers.co.kr/learn/courses/30/lessons/42628
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
이름은 우선순위 큐 지만 multiset쓰는게 훨 편하다. 앞뒤로 빼야하기 때문
#include <string>
#include <vector>
#include <set>
// #include <iostream>
using namespace std;
vector<int> solution(vector<string> operations) {
vector<int> answer;
multiset<int> ms;
for (int i = 0; i < operations.size(); i++) {
if (operations[i][0] == 'I') {
ms.insert(stoi(operations[i].substr(2))); // substr(숫자) 넣으면 숫자만큼 앞에 자른다
// cout << stoi(operations[i].substr(2)) << endl;
}
else if (operations[i][2] == '-' && ms.size())
ms.erase(ms.begin());
else if (operations[i][2] == '1' && ms.size())
ms.erase(--ms.end()); // 이렇게 하면 맨 마지막 요소 삭제 가능!!!!
}
if (ms.size() == 0) {
answer.push_back(0);
answer.push_back(0);
return answer;
}
answer.push_back(*(--ms.end()));
answer.push_back(*(ms.begin()));
return answer;
}
반응형
'<algorithm> > 프로그래머스' 카테고리의 다른 글
프로그래머스 [3차] n진수 게임 c++ (0) | 2023.08.30 |
---|---|
프로그래머스 압축 c++ (4) | 2023.08.07 |
프로그래머스 야근 지수 c++ (0) | 2023.07.30 |
프로그래머스 네트워크 c++ (2) | 2023.07.03 |
프로그래머스 타겟넘버 c++ (2) | 2023.07.02 |