문제
https://school.programmers.co.kr/learn/courses/30/lessons/42628
그냥 map 씀
- 맵은 key 값으로 자동정렬이고 빨라서 최대, 최소 찾으면 됨
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
vector<int> solution(vector<string> operations) {
vector<int> answer;
multimap<int, int> map;
for (auto v : operations)
{
string op = v.substr(0,1);
if (op == "I")
{
string num = v.substr(2,v.length() - 2);
int n = stoi(num);
map.insert({n, n});
}
else if (op == "D" && map.empty() == false)
{
string num = v.substr(2,v.length() - 2);
int n = stoi(num);
if (n == 1)
{
auto m = max_element(map.begin(), map.end());
map.erase(m);
}
else if (n == -1)
{
auto m = min_element(map.begin(), map.end());
map.erase(m);
}
}
}
answer.emplace_back((*max_element(map.begin(), map.end())).first);
answer.emplace_back((*min_element(map.begin(), map.end())).first);
return answer;
}