https://programmers.co.kr/learn/courses/30/lessons/42628#
#include <string>
#include <vector>
#include <queue>
using namespace std;
vector<int> solution(vector<string> operations) {
vector<int> answer;
vector<string> cmd = {"D 1", "D -1"};
priority_queue<int, vector<int>, less<int>> pql; // 내림
priority_queue<int, vector<int>, greater<int>> pqg; // 오름
int cnt = 0;
for(int i=0;i<operations.size();i++){
string s = operations[i];
if(cnt == 0){ // 비우기
while(!pql.empty())
pql.pop();
while(!pqg.empty())
pqg.pop();
}
if(s == cmd[0]){ // 최댓값
if(cnt != 0){
pql.pop();
cnt--;
}
}else if (s == cmd[1]){ // 최솟값
if(cnt != 0){
pqg.pop();
cnt--;
}
}else{ // 숫자 push
int num = stoi(s.substr(2, s.size()-2));
pql.push(num);
pqg.push(num);
cnt++;
}
}
if(cnt == 0)
return {0, 0};
else
return {pql.top(), pqg.top()};
}
#include <string>
#include <vector>
#include <set>
using namespace std;
vector<int> solution(vector<string> arguments) {
vector<int> answer;
multiset<int> que;
multiset<int>::iterator iter;
string sub;
for(auto s : arguments) {
sub =s.substr(0, 2);
if(sub=="I ") que.insert(stoi(s.substr(2,s.length()-2)));
else if(s.substr(2,1)=="1"&&que.size()>0) { que.erase(--que.end()); }
else if(que.size()>0) { que.erase(que.begin()); }
}
if(que.size()==0) { answer.push_back(0); answer.push_back(0); }
else {
iter = --que.end(); answer.push_back(*iter);
iter = que.begin(); answer.push_back(*iter);
}
return answer;
}
#include <string>
#include <vector>
#include <set>
using namespace std;
vector<int> solution(vector<string> operations) {
vector<int> answer;
multiset<int> ms;
for(auto o : operations){
if(o == "D 1" && ms.size()>0){
ms.erase(--ms.end());
}else if(o == "D -1" && ms.size()>0){
ms.erase(ms.begin());
}else if(o[0] == 'I'){
ms.insert(stoi(o.substr(2, o.size()-2)));
}
}
if(ms.size() == 0)
return {0, 0};
else
return {*(--ms.end()), *ms.begin()};
}